本文共 5366 字,大约阅读时间需要 17 分钟。
在java中synchronized是一个非常重要的关键字,它主要用来控制线程同步的,在多线程环境下能实现临界区资源的互斥访问。
synchronized关键字最主要有以下3种应用方式,下面分别介绍
a.修饰实例方法
class SafeAdd implements Runnable { public static int num = 0; public synchronized void add() { num ++; } @Override public void run() { for (int i = 0; i < 10000; ++i) { add(); } }}public class SyncMethod { public static void main(String[] args) throws InterruptedException { SafeAdd safeAdd = new SafeAdd(); Thread t1 = new Thread(safeAdd); Thread t2 = new Thread(safeAdd); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println("SafeAdd: num = " + SafeAdd.num); }}
运行结果:
SafeAdd: num = 20000Process finished with exit code 0
b.修饰静态方法
class SafeAdd implements Runnable { public static int num = 0; public static synchronized void add() { num ++; } @Override public void run() { for (int i = 0; i < 10000; ++i) { add(); } }}public class SyncMethod { public static void main(String[] args) throws InterruptedException { SafeAdd safeAdd1 = new SafeAdd(); SafeAdd safeAdd2 = new SafeAdd(); Thread t1 = new Thread(safeAdd1); Thread t2 = new Thread(safeAdd2); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println("SafeAdd: num = " + SafeAdd.num); }}
运行结果:
SafeAdd: num = 20000Process finished with exit code 0
c.同步代码块
class SafeAdd implements Runnable { public static int num = 0; @Override public void run() { synchronized (this) { for (int i = 0; i < 10000; ++i) { num ++; } } }}public class SyncMethod { public static void main(String[] args) throws InterruptedException { SafeAdd safeAdd = new SafeAdd(); Thread t1 = new Thread(safeAdd); Thread t2 = new Thread(safeAdd); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println("SafeAdd: num = " + SafeAdd.num); }}
运行结果:
SafeAdd: num = 20000Process finished with exit code 0
d.同步代码块,加锁对象为Class对象
class SafeAdd implements Runnable { public static int num = 0; public static void add() { num ++; } @Override public void run() { synchronized (SafeAdd.class) { for (int i = 0; i < 10000; ++i) { add(); } } }}public class SyncMethod { public static void main(String[] args) throws InterruptedException { SafeAdd safeAdd1 = new SafeAdd(); SafeAdd safeAdd2 = new SafeAdd(); Thread t1 = new Thread(safeAdd1); Thread t2 = new Thread(safeAdd2); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println("SafeAdd: num = " + SafeAdd.num); }}
运行结果:
SafeAdd: num = 20000Process finished with exit code 0
synchronized的实现原理可以通过反编译代码来查看,下面看下synchronized同步代码块
如何实现同步。public class SynchronizedTest { public void method() { synchronized (this) { System.out.println("SynchronizedTest"); } }}
反编译结果:
关于这两条指令的作用可以参考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的所有者。
接着看下synchronized同步方法的反编译结果:
javap -v SynchronizedTest.class从反编译的结果来看,方法的同步并没有通过指令monitorenter和monitorexit来完成(理论上其实也可以通过这两条指令来实现),不过相对于普通方法,其常量池中多了ACC_SYNCHRONIZED标示符。JVM就是根据该标示符来实现方法的同步的:当方法调用时,调用指令将会检查方法的 ACC_SYNCHRONIZED 访问标志是否被设置,如果设置了,执行线程将先获取monitor,获取成功之后才能执行方法体,方法执行完后再释放monitor。在方法执行期间,其他任何线程都无法再获得同一个monitor对象。 其实本质上没有区别,只是方法的同步是一种隐式的方式来实现,无需通过字节码来完成。
转载地址:http://xecmb.baihongyu.com/