Synchronized的用法
在多线程并发问题中,常用Synchronized锁解决问题。Synchronized锁通常用于同步示例方法,同步静态方法,同步代码块等。
同步示例方法
我们可能自己使用过在方法前加Synchronized锁修饰,在多线程并发同时调用同一个实例化对象时,如果这个方法加上了Synchronized锁,那么也是线程安全的。
举个栗子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package Thread; import java.util.stream.IntStream; public class ThreadTest { private Long count=0L; public void incrementCount(){ count++; } public Long execute() throws InterruptedException { Thread thread1= new Thread(()->{ IntStream.range( 0 , 1000 ).forEach((i)->incrementCount()); //线程1循环1000次 }); Thread thread2= new Thread(()->{ IntStream.range( 0 , 1000 ).forEach((i)->incrementCount()); //线程2循环1000次 }); thread1.start(); //开启线程 thread2.start(); thread1.join(); //等待线程1和线程2执行完毕 thread2.join(); return count; } public static void main(String[] args) throws InterruptedException { ThreadTest threadTest= new ThreadTest(); Long count = threadTest.execute(); System.out.println(count); } } |
在上面的程序中,count变量为成员变量,在多线程同时使用时极大可能会发生错误,在前面也讲到过count++包含三个步骤:1.将变量count从主内存中加载到CPU的寄存器中;2.在CPU的寄存器中执行count++或++count的操作;3.将运算的count++的结果写入缓存或内存中。两个线程都会更新count的值到内存中,当其中一个线程再从内存中读取数据时,可能读到的成员变量会与当前的变量不一致,从而使得最终count的结果不为2000,因此会发生错误。
如何能解决这种错误?就是为incrementCount方法加锁:
1 2 3 | public synchronized void incrementCount(){ count++; } |
这样就能保证所得到的count最终值为2000了。
同步静态方法
当一个类的某个静态方法加了synchronized锁时,就相当于给这个类的class对象加锁。所以无论创建多少个当前类的对象调用这个被synchronized锁修饰的静态方法时,都是线程安全的。
如上面的例子,修改如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package Thread; import java.util.stream.IntStream; public class ThreadTest { private static Long count=0L; public static synchronized void incrementCount(){ count++; } public static Long execute() throws InterruptedException { Thread thread1= new Thread(()->{ IntStream.range( 0 , 1000 ).forEach((i)->incrementCount()); }); Thread thread2= new Thread(()->{ IntStream.range( 0 , 1000 ).forEach((i)->incrementCount()); }); thread1.start(); thread2.start(); thread1.join(); thread2.join(); return count; } public static void main(String[] args) throws InterruptedException { ThreadTest threadTest= new ThreadTest(); Long count = threadTest.execute(); System.out.println(count); } } |
因此,当多个线程并发执行调用被synchronized锁修饰的静态方法时,这个静态方法是线程安全的。
同步代码块
前面提到加了synchronized锁的方法在多线程并发条件下是线程安全的,但是在执行业务逻辑过多的代码块时,可能会影响程序的执行效率。对于此时,可以把一个方法分成多个小的临界区。
举个栗子:
1 2 3 4 5 6 | private Long count1=0L; private Long count2=0L; public synchronized void incrementCount(){ count1++; count2++; } |
在上面的代码中,count1和count2为两个不同的自增操作,因此对于count1和count2来说是两个不同的临界区资源。当一个线程进入incrementCount方法中时,会对整个方法进行加锁,在对count1进行自增操作时,也会占用count2的资源,相当于占用全部的资源。只有等到这个线程执行完count1++和count2++的操作时,释放锁时,其它线程才能拿到锁资源进入incrementCount方法。
但是这样会影响程序的性能。因为count1++和count2++为两个互不影响的两个临界区资源,当线程拿到锁,会占用两个资源,使得临界区资源进行闲置等待,因此可以优化代码,让synchronized锁修饰代码块。
修改后的代码:
1 2 3 4 5 6 7 8 9 10 11 12 | private Long count1=0L; private Long count2=0L; public Object count1Lock= new Object(); public Object count2Lock= new Object(); public void incrementCount(){ synchronized (count1Lock){ count1++; } synchronized (count2Lock){ count2++; } } |
上面代码中,对count1和count2分别建立了对象锁count1Lock和count2Lock,而没有对incrementCount加锁,意为当一个线程进入incrementCount方法时,其他线程也能进入此方法,当线程1拿到count1Lock对象锁时,不影响线程2拿到count2Lock对象锁来对count2执行自增操作。
这样既提高了程序的执行效率,同时,由于临界区资源都加了锁,incrementCount方法也是线程安全的。
到此这篇关于Java Synchronized锁的使用详解的文章就介绍到这了,更多相关Java Synchronized锁内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!