synchronized

What is the difference between synchronized on lockObject and using this as the lock?

試著忘記壹切 提交于 2019-12-17 04:18:06
问题 I know the difference between synchronized method and synchronized block but I am not sure about the synchronized block part. Assuming I have this code class Test { private int x=0; private Object lockObject = new Object(); public void incBlock() { synchronized(lockObject) { x++; } System.out.println("x="+x); } public void incThis() { // same as synchronized method synchronized(this) { x++; } System.out.println("x="+x); } } In this case what is the difference between using lockObject and

What is the difference between synchronized on lockObject and using this as the lock?

孤者浪人 提交于 2019-12-17 04:18:04
问题 I know the difference between synchronized method and synchronized block but I am not sure about the synchronized block part. Assuming I have this code class Test { private int x=0; private Object lockObject = new Object(); public void incBlock() { synchronized(lockObject) { x++; } System.out.println("x="+x); } public void incThis() { // same as synchronized method synchronized(this) { x++; } System.out.println("x="+x); } } In this case what is the difference between using lockObject and

线程安全

旧巷老猫 提交于 2019-12-17 03:24:52
1.线程安全概念: 当 多个线程访问某一个类 (对象或方法)时,这个类始终都能表现出正确的行为,那么这个类(对象或方法)就是线程安全的. synchronized:可以在任意对象及方法上加锁,而加锁的这段代码称为"互斥区"或"临界区" 2.实例:MyTread 上面图是没加synorized的情况. 当多个线程访问myThread的润方法时,以排队的方式进行处理(这里的排队是按照CPU分配的先后顺序而定的),一个线程想要执行synchronized修饰的方法里的代码,首先是尝试获得锁,如果拿到锁,执行synchronized代码提内容;拿不到锁,这个线程就会不断的尝试获得这把锁,直到拿到为止, 而且是多个线程同时去竞争这把锁.(也就是会有锁竞争的问题) 应当避免多线程竞争的问题,因为这会导致CPU使用率急剧上升 来源: https://www.cnblogs.com/curedfisher/p/11977952.html

Java Synchronized Block for .class

无人久伴 提交于 2019-12-17 03:22:34
问题 What does this java code mean? Will it gain lock on all objects of MyClass ? synchronized(MyClass.class) { //is all objects of MyClass are thread-safe now ?? } And how the above code differs from this one: synchronized(this) { //is all objects of MyClass are thread-safe now ?? } 回答1: The snippet synchronized(X.class) uses the class instance as a monitor. As there is only one class instance (the object representing the class metadata at runtime) one thread can be in this block. With

Happens-before relationships with volatile fields and synchronized blocks in Java - and their impact on non-volatile variables?

偶尔善良 提交于 2019-12-17 03:00:43
问题 I am still pretty new to the concept of threading, and try to understand more about it. Recently, I came across a blog post on What Volatile Means in Java by Jeremy Manson, where he writes: When one thread writes to a volatile variable, and another thread sees that write, the first thread is telling the second about all of the contents of memory up until it performed the write to that volatile variable. [...] all of the memory contents seen by Thread 1, before it wrote to [volatile] ready ,

双重检测锁

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 00:55:52
双重检测锁 1、 首先,一听这个名称的时候,我是一脸茫然,下面我就来介绍一下今天学习双重检测锁的收获 吧,今天在在写代码的时候遇到一个 高并发 的情况,就是我写的接口如果同时有一万个人同时访问得话,那么公司的系统能不能承受的住的这么大的压力呢? 一开始想到是直接在方法上加锁synchronized,这是最简单的方法。例如: @GetMapping("/") public synchronized List<Salary> getAllSalarys(){ return salaryService.getAllSalarys(); } 在方法名上加锁的意思是,每次我只允许一个线程去访问,如果线程在运行中那就一次等待。 2、使用双重检测锁 public List<Student> getAllStudent() { //KEY 是按照字符串方式序列化,可读性较好 redisTemplate.setKeySerializer(keyRedisSerializer); //在高并发条件下,会出现缓存穿透 List<Student> studentList = (List<Student>)redisTemplate.opsForValue().get("allStudent"); if (null == studentList) { //5个人, 4个等,1个进入 synchronized

多线程基础知识

亡梦爱人 提交于 2019-12-16 22:07:40
线程与进程的区别   1. 进程是资源分配的最小单元,线程是CPU调度的最小单元。所有与进程相关的资源,均被记录再PCB中。   2. 线程隶属于某一个进程,共享所有进程的资源。线程只由堆栈寄存器、程序计数器和TCB构成。   3. 进程可以看作独立的应用,线程不能看作独立的应用。   4. 进程有独立的地址空间,相互不影响,而线程只是进程的不同执行路径,如果线程挂了,进程也就挂了。所以多进程的程序比多线程程序健壮,但是切换消耗资源多。    Java中进程与线程的关系   1. 运行一个程序会产生一个进程,进程至少包含一个线程。   2. 每个进程对应一个JVM实例,多线线程共享JVM中的堆。   3. Java采用单线程编程模型,程序会自动创建主线程。   4. 主线程可以创建子线程,原则上要后于子线程完成执行。 线程中start方法和run方法的区别   Java中创建线程的方式有两种,不管使用继承Thread的方法是hiRunnable接口的方法,都需要重写run方法。调用start方法会创建一个新的线程并启动,run方法只是启动线程后的回调函数,如果调用run方法,那么执行run方法的线程不会是新创建的线程,而如果使用start方法,那么执行run方法的线程就是我们刚刚启动的那个线程。 public class Main { public static void main

Java多线程 线程同步

。_饼干妹妹 提交于 2019-12-16 22:03:17
如果你正在写一个变量,它可能接下来将被另一个线程读取,或者正在读取一个上一次已经被另一个线程写过的变量,那么你需要使用同步,并且,读写线程都必须用相同的监视器锁同步。--Brain同步规则 synchronized 所有对象都自动含有单一的锁,当在调用一个对象的任意synchronized方法时,此对象将被加锁。 对于某个特定对象来说,所有的synchronized方法共享同一个锁。所以某个线程在访问对象的一个synchronized方法时,其他线程访问该对象的任何synchronized方法都将被阻塞。 一个任务可以多次获得对象锁,如在调用对象的一个方法时,该方法又调用了其他的方法。JVM负责跟踪对象被加锁的次数,这也是锁的可重入性,不可重入的锁可能造成死锁。 每个类也有一个锁,每个类都是一个class对象,synchronized static方法可以防止对static数据的并发访问 在使用并发时,最好将字段设置为private,防止线程直接访问字段。 可重入锁ReentrantLock 在java.util.concurrent.locks包中定义了显式的Lock,该Lock锁需要显式的创建、锁定和释放。比起synchronized关键字,显式的Lock锁使用起来更繁琐,在使用时也更有可能出错,但有更强大的功能

线程常用25道题

谁都会走 提交于 2019-12-16 15:04:35
并发编程三要素? 1)原子性 原子性指的是一个或者多个操作,要么全部执行并且在执行的过程中不被其他操作打断,要么就全部都不执行。 2)可见性 可见性指多个线程操作一个共享变量时,其中一个线程对变量进行修改后,其他线程可以立即看到修改的结果。 3)有序性 有序性,即程序的执行顺序按照代码的先后顺序来执行。 Thread.sleep(0)的作用是什么? 由于Java采用抢占式的线程调度算法,因此可能会出现某条线程常常获取到CPU控制权的情况,为了让某些优先级比较低的线程也能获取到CPU控制权,可以使用Thread.sleep(0)手动触发一次操作系统分配时间片的操作,这也是平衡CPU控制权的一种操作。 java中的++操作符线程安全么?? 不是线程安全的操作。它涉及到多个指令,如读取变量值,增加,然后存储回内存,这个过程可能会出现多个线程交差 Runnable和Callable有什么区别? Callable规定(重写)的方法是call(),Runnable规定(重写)的方法是run()。 Callable的任务执行后可返回值,而Runnable的任务是不能返回值的。 Call方法可以抛出异常,run方法不可以。 运行Callable任务可以拿到一个Future对象,表示异步计算的结果。它提供了检查计算是否完成的方法,以等待计算的完成,并检索计算的结果

80道最新java基础部分面试题(五)

懵懂的女人 提交于 2019-12-16 12:00:34
自己整理的面试题,希望可以帮到大家,需要更多资料的可以私信我哦,大家一起学习进步! 48、同步和异步有何异同,在什么情况下分别使用他们?举例说明。 如果数据将在线程间共享。例如正在写的数据以后可能被另一个线程读到,或者正在读的数据可能已经被另一个线程写过了,那么这些数据就是共享数据,必须进行同步存取。 当应用程序在对象上调用了一个需要花费很长时间来执行的方法,并且不希望让程序等待方法的返回时,就应该使用异步编程,在很多情况下采用异步途径往往更有效率。 下面两个方法同步吗?(自己发明) class Test { synchronized static void sayHello3() { } synchronized void getX(){} } 50、多线程有几种实现方法?同步有几种实现方法? 多线程有两种实现方法,分别是继承Thread类与实现Runnable接口 同步的实现方面有两种,分别是synchronized,wait与notify wait():使一个线程处于等待状态,并且释放所持有的对象的lock。 sleep():使一个正在运行的线程处于睡眠状态,是一个静态方法,调用此方法要捕捉InterruptedException异常。 notify():唤醒一个处于等待状态的线程,注意的是在调用此方法的时候,并不能确切的唤醒某一个等待状态的线程,而是由JVM确定唤醒哪个线程