深刻理解多线程(一)——Synchronized的实现原理

synchronized,是Java中用于解决并发状况下数据同步访问的一个很重要的关键字。当咱们想要保证一个共享资源在同一时间只会被一个线程访问到时,咱们能够在代码中使用synchronized关键字对类或者对象加锁。那么,本文来介绍一下synchronized关键字的实现原理是什么。在阅读本文之间,建议先看下Java虚拟机是如何执行线程同步的html

反编译

众所周知,在Java中,synchronized有两种使用形式,同步方法和同步代码块。代码以下:java

/**
 * @author Hollis 17/11/9.
 */
public class SynchronizedTest {

    public synchronized void doSth(){
        System.out.println("Hello World");
    }

    public void doSth1(){
        synchronized (SynchronizedTest.class){
            System.out.println("Hello World");
        }
    }
}
复制代码

咱们先来使用Javap来反编译以上代码,结果以下(部分无用信息过滤掉了):并发

public synchronized void doSth();
    descriptor: ()V
    flags: ACC_PUBLIC, ACC_SYNCHRONIZED
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #3                  // String Hello World
         5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: return

  public void doSth1();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=3, args_size=1
         0: ldc           #5                  // class com/hollis/SynchronizedTest
         2: dup
         3: astore_1
         4: monitorenter
         5: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         8: ldc           #3                  // String Hello World
        10: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        13: aload_1
        14: monitorexit
        15: goto          23
        18: astore_2
        19: aload_1
        20: monitorexit
        21: aload_2
        22: athrow
        23: return
复制代码

反编译后,咱们能够看到Java编译器为咱们生成的字节码。在对于doSthdoSth1的处理上稍有不一样。也就是说。JVM对于同步方法和同步代码块的处理方式不一样。oracle

对于同步方法,JVM采用ACC_SYNCHRONIZED标记符来实现同步。 对于同步代码块。JVM采用monitorentermonitorexit两个指令来实现同步。jvm

关于这部份内容,在JVM规范中也能够找到相关的描述。ui

同步方法

The Java® Virtual Machine Specification中有关于方法级同步的介绍:spa

Method-level synchronization is performed implicitly, as part of method invocation and return. A synchronized method is distinguished in the run-time constant pool's method_info structure by the ACC_SYNCHRONIZED flag, which is checked by the method invocation instructions. When invoking a method for which ACC_SYNCHRONIZED is set, the executing thread enters a monitor, invokes the method itself, and exits the monitor whether the method invocation completes normally or abruptly. During the time the executing thread owns the monitor, no other thread may enter it. If an exception is thrown during invocation of the synchronized method and the synchronized method does not handle the exception, the monitor for the method is automatically exited before the exception is rethrown out of the synchronized method.线程

主要说的是: 方法级的同步是隐式的。同步方法的常量池中会有一个ACC_SYNCHRONIZED标志。当某个线程要访问某个方法的时候,会检查是否有ACC_SYNCHRONIZED,若是有设置,则须要先得到监视器锁,而后开始执行方法,方法执行以后再释放监视器锁。这时若是其余线程来请求执行方法,会由于没法得到监视器锁而被阻断住。值得注意的是,若是在方法执行过程当中,发生了异常,而且方法内部并无处理该异常,那么在异常被抛到方法外面以前监视器锁会被自动释放。code

同步代码块

同步代码块使用monitorentermonitorexit两个指令实现。 The Java® Virtual Machine Specification 中有关于这两个指令的介绍:orm

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.

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.

大体内容以下: 能够把执行monitorenter指令理解为加锁,执行monitorexit理解为释放锁。 每一个对象维护着一个记录着被锁次数的计数器。未被锁定的对象的该计数器为0,当一个线程得到锁(执行monitorenter)后,该计数器自增变为 1 ,当同一个线程再次得到该对象的锁的时候,计数器再次自增。当同一个线程释放锁(执行monitorexit指令)的时候,计数器再自减。当计数器为0的时候。锁将被释放,其余线程即可以得到锁。

总结

同步方法经过ACC_SYNCHRONIZED关键字隐式的对方法进行加锁。当线程要执行的方法被标注上ACC_SYNCHRONIZED时,须要先得到锁才能执行该方法。

同步代码块经过monitorentermonitorexit执行来进行加锁。当线程执行到monitorenter的时候要先得到所锁,才能执行后面的方法。当线程执行到monitorexit的时候则要释放锁。

每一个对象自身维护这一个被加锁次数的计数器,当计数器数字为0时表示能够被任意线程得到锁。当计数器不为0时,只有得到锁的线程才能再次得到锁。便可重入锁。

至此,咱们大体了解了Synchronized的原理。可是还有几个问题并无介绍清楚,好比,Monitor究竟是什么?对象的锁的状态保存在哪里? 别急,后面会再介绍。

相关文章
相关标签/搜索