做者:iuxiaopeng
https://www.cnblogs.com/paddi...
Synchronized 的基本使用html
Synchronized 的做用主要有三个:java
从语法上讲,Synchronized 总共有三种用法:面试
接下来我就经过几个例子程序来讲明一下这三种使用方式(为了便于比较,三段代码除了 Synchronized 的使用方式不一样之外,其余基本保持一致)。编程
没有同步的状况后端
代码段 1:安全
package com.paddx.test.concurrent; public class SynchronizedTest { public void method1(){ System.out.println("Method 1 start"); try { System.out.println("Method 1 execute"); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Method 1 end"); } public void method2(){ System.out.println("Method 2 start"); try { System.out.println("Method 2 execute"); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Method 2 end"); } public static void main(String\[\] args) { final SynchronizedTest test = new SynchronizedTest(); new Thread(new Runnable() { @Override public void run() { test.method1(); } }).start(); new Thread(new Runnable() { @Override public void run() { test.method2(); } }).start(); } }
执行结果以下,线程 1 和线程 2 同时进入执行状态,线程 2 执行速度比线程 1 快,因此线程 2 先执行完成。推荐阅读:多线程 start 和 run 方法到底有什么区别?多线程
这个过程当中线程 1 和线程 2 是同时执行的:架构
Method 1 start Method 1 execute Method 2 start Method 2 execute Method 2 end Method 1 end
对普通方法同步并发
代码段 2:ide
package com.paddx.test.concurrent; public class SynchronizedTest { public synchronized void method1(){ System.out.println("Method 1 start"); try { System.out.println("Method 1 execute"); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Method 1 end"); } public synchronized void method2(){ System.out.println("Method 2 start"); try { System.out.println("Method 2 execute"); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Method 2 end"); } public static void main(String\[\] args) { final SynchronizedTest test = new SynchronizedTest(); new Thread(new Runnable() { @Override public void run() { test.method1(); } }).start(); new Thread(new Runnable() { @Override public void run() { test.method2(); } }).start(); } }
执行结果以下,跟代码段 1 比较,能够很明显的看出,线程 2 须要等待线程 1 的 Method1 执行完成才能开始执行 Method2 方法。
Method 1 start Method 1 execute Method 1 end Method 2 start Method 2 execute Method 2 end
静态方法(类)同步
代码段 3:
package com.paddx.test.concurrent; public class SynchronizedTest { public static synchronized void method1(){ System.out.println("Method 1 start"); try { System.out.println("Method 1 execute"); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Method 1 end"); } public static synchronized void method2(){ System.out.println("Method 2 start"); try { System.out.println("Method 2 execute"); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Method 2 end"); } public static void main(String\[\] args) { final SynchronizedTest test = new SynchronizedTest(); final SynchronizedTest test2 = new SynchronizedTest(); new Thread(new Runnable() { @Override public void run() { test.method1(); } }).start(); new Thread(new Runnable() { @Override public void run() { test2.method2(); } }).start(); } }
执行结果以下,对静态方法的同步本质上是对类的同步(静态方法本质上是属于类的方法,而不是对象上的方法)。
因此即便 Test 和 Test2 属于不一样的对象,可是它们都属于 SynchronizedTest 类的实例。
因此也只能顺序的执行 Method1 和 Method2,不能并发执行:
Method 1 start Method 1 execute Method 1 end Method 2 start Method 2 execute Method 2 end
代码块同步
代码段 4:
package com.paddx.test.concurrent; public class SynchronizedTest { public void method1(){ System.out.println("Method 1 start"); try { synchronized (this) { System.out.println("Method 1 execute"); Thread.sleep(3000); } } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Method 1 end"); } public void method2(){ System.out.println("Method 2 start"); try { synchronized (this) { System.out.println("Method 2 execute"); Thread.sleep(1000); } } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Method 2 end"); } public static void main(String\[\] args) { final SynchronizedTest test = new SynchronizedTest(); new Thread(new Runnable() { @Override public void run() { test.method1(); } }).start(); new Thread(new Runnable() { @Override public void run() { test.method2(); } }).start(); } }
执行结果以下,虽然线程 1 和线程 2 都进入了对应的方法开始执行,可是线程 2 在进入同步块以前,须要等待线程 1 中同步块执行完成。
Method 1 start Method 1 execute Method 2 start Method 1 end Method 2 execute Method 2 end
Synchronized 原理
若是对上面的执行结果还有疑问,也先不用急,咱们先来了解 Synchronized 的原理。推荐阅读:面试常考:Synchronized 有几种用法?
再回头上面的问题就一目了然了。咱们先经过反编译下面的代码来看看 Synchronized 是如何实现对代码块进行同步的:
package com.paddx.test.concurrent; public class SynchronizedMethod { public synchronized void method() { System.out.println("Hello World!"); } }
反编译结果:
关于这两条指令的做用,咱们直接参考 JVM 规范中描述:
monitorenter :Each object is associated with a monitor. A monitor is locked if and only if it has an owner. The thread that executes monitorenter attempts to gain ownership of the monitor associated with objectref, as follows:
• If the entry count of the monitor associated with objectref is zero, the thread enters the monitor and sets its entry count to one. The thread is then the owner of the monitor.
• If the thread already owns the monitor associated with objectref, it reenters the monitor, incrementing its entry count.
• If another thread already owns the monitor associated with objectref, the thread blocks until the monitor's entry count is zero, then tries again to gain ownership.
这段话的大概意思为:每一个对象有一个监视器锁(Monitor),当 Monitor 被占用时就会处于锁定状态。
线程执行 Monitorenter 指令时尝试获取 Monitor 的全部权,过程以下:
monitorexit:The thread that executes monitorexit must be the owner of the monitor associated with the instance referenced by objectref.
The thread decrements the entry count of the monitor associated with objectref. If as a result the value of the entry count is zero, the thread exits the monitor and is no longer its owner.
Other threads that are blocking to enter the monitor are allowed to attempt to do so.
这段话的大概意思为:执行 Monitorexit 的线程必须是 Objectref 所对应的 Monitor 的全部者。
指令执行时,Monitor 的进入数减 1,若是减 1 后进入数为 0,那线程退出 Monitor,再也不是这个 Monitor 的全部者。
其余被这个 Monitor 阻塞的线程能够尝试去获取这个 Monitor 的全部权。
经过这两段描述,咱们应该能很清楚的看出 Synchronized 的实现原理。
Synchronized 的语义底层是经过一个 Monitor 的对象来完成,其实 Wait/Notify 等方法也依赖于 Monitor 对象。推荐阅读:Synchronized 与 ReentrantLock 的区别!
这就是为何只有在同步的块或者方法中才能调用 Wait/Notify 等方法,不然会抛出 java.lang.IllegalMonitorStateException 的异常。
咱们再来看一下同步方法的反编译结果,源代码以下:
package com.paddx.test.concurrent; public class SynchronizedMethod { public synchronized void method() { System.out.println("Hello World!"); } }
反编译结果:
从反编译的结果来看,方法的同步并无经过指令 Monitorenter 和 Monitorexit 来完成(理论上其实也能够经过这两条指令来实现)。不过相对于普通方法,其常量池中多了 ACC_SYNCHRONIZED 标示符。
JVM 就是根据该标示符来实现方法的同步的:当方法调用时,调用指令将会检查方法的 ACC_SYNCHRONIZED 访问标志是否被设置。
若是设置了,执行线程将先获取 Monitor,获取成功以后才能执行方法体,方法执行完后再释放 Monitor。在方法执行期间,其余任何线程都没法再得到同一个 Monitor 对象。
其实本质上没有区别,只是方法的同步是一种隐式的方式来实现,无需经过字节码来完成。
运行结果解释
有了对 Synchronized 原理的认识,再来看上面的程序就能够迎刃而解了。
①代码段 2 结果
虽然 Method1 和 Method2 是不一样的方法,可是这两个方法都进行了同步,而且是经过同一个对象去调用的。
因此调用以前都须要先去竞争同一个对象上的锁(Monitor),也就只能互斥的获取到锁,所以,Method1 和 Method2 只能顺序的执行。
②代码段 3 结果
虽然 Test 和 Test2 属于不一样对象,可是 Test 和 Test2 属于同一个类的不一样实例。
因为 Method1 和 Method2 都属于静态同步方法,因此调用的时候须要获取同一个类上 Monitor(每一个类只对应一个 Class 对象),因此也只能顺序的执行。
③代码段 4 结果
对于代码块的同步,实质上须要获取 Synchronized 关键字后面括号中对象的 Monitor。
因为这段代码中括号的内容都是 This,而 Method1 和 Method2 又是经过同一的对象去调用的,因此进入同步块以前须要去竞争同一个对象上的锁,所以只能顺序执行同步块。
总结
Synchronized 是 Java 并发编程中最经常使用的用于保证线程安全的方式,其使用相对也比较简单。
可是若是可以深刻了解其原理,对监视器锁等底层知识有所了解,一方面能够帮助咱们正确的使用 Synchronized 关键字。
另外一方面也可以帮助咱们更好的理解并发编程机制,有助于咱们在不一样的状况下选择更优的并发策略来完成任务。对平时遇到的各类并发问题,也可以从容的应对。
推荐去个人博客阅读更多:
2.Spring MVC、Spring Boot、Spring Cloud 系列教程
3.Maven、Git、Eclipse、Intellij IDEA 系列工具教程
以为不错,别忘了点赞+转发哦!