synchronized实现线程同步的用法和实现原理

版权声明:非商业目的可自由转载,转载请标明出处 https://blog.csdn.net/u010013573/article/details/87219343

作用和用法

  • 在多线程对共享资源进行并发访问方面,JDK提供了synchronized关键字来进行线程同步,实现多线程并发访问的线程安全。synchronized的作用主要体现在三个方面:(1)确保线程互斥地访问同步代码;(2)保证共享变量的线程可见性;(3)禁止指令重排。其中(2)和(3)相当于volatile关键字的作用。

  • sychronized可以用在(1)静态方法,(2)普通成员方法,(3)代码块上:

    • 静态方法:将类对象自身作为monitor对象,对该类所有使用了sychronized修饰的静态方法进行同步,即任何时候只能存在一个线程在调用该类的使用了synchronized修饰的静态方法,其他调用了该类的使用了synchronized修饰的静态方法的线程需要阻塞;
    • 普通成员方法:使用类的对象实例作为monitor对象,该类所有使用了synchronized修饰的成员方法,在任何时刻只能被一个线程访问,其他线程需要阻塞;
    • 代码块:使用某个对象作为monitor对象,通常为一个普通的private成员变量,如private Object object = new Object();,这样所有使用了该object对象的同步块,在任何时候只能存在一个线程访问。
  • synchronized可以与monitor对象的wait,notify,notifyAll方法一起来使用,实现线程之间的通信,如实现生产者和消费者模型。其中多个线程共享一个monitor对象,在线程持有synchronized锁时,才能调用monitor的wait,notify或者notifyAll,分别用于释放monitor锁,阻塞休眠,等待其他线程;通知和唤醒其中一个阻塞休眠的线程,让该线程去获取monitor锁;通知所有阻塞休眠的线程去竞争monitor锁。

  • synchronized使用方便,无需显示地在应用代码中加锁和解锁,只需在对应的方法或者代码块中使用synchronized关键字修饰即可,由JVM自身实现自动地加锁和释放锁。

  • synchronized修饰的范围越小,线程并发度越高,性能越好,所以通常使用同步代码块,而不是同步方法来缩小同步范围,优化性能。

实现原理

JVM层面
  • synchronized关键字是基于JVM提供的monitorenter和monitorexit字节码指令,以及结合监视器monitor来实现的。

  • 由上面的分析可知,synchronized关键字用在静态方法,普通成员方法,代码块中,分别需要以类对象自身,类的对象实例,某个普通对象作为对应的monitor对象。

  • 由JVM的相关知识可知,任何java类都需要编译成class字节码,然后加载到JVM当中去执行。而在编译一个java类生成对应class字节码时,当遇到sychronized关键字时,会在sychronized关键字所修饰的方法或者代码块的开始处:增加一个monitorenter字节码指令,在方法或者代码块的结束处:增加monitorexit字节码指令,即使用monitorenter和monitorexit字节码指令包围该方法或者代码块对应的字节码。如下:

    • 在类的成员方法中使用synchronized关键字:

      package com.yzxie.easy.log.web;
      
      /**
       * @author xyz
       * @date 13/2/2019 22:50
       * @description:
       */
      public class SynchronizedTest {
      
          public void method() {
              synchronized (this) {
                  System.out.println("Hello world");
              }
          }
      }
      
    • 反编译该类对应的class字节码文件:在成员方法method对应的字节码周围使用了monitorenter和monitorexit字节码指令。

      xyzdeMacBook-Pro:test-classes xyz$ javap -c com.yzxie.easy.log.web.SynchronizedTest
      Compiled from "SynchronizedTest.java"
      public class com.yzxie.easy.log.web.SynchronizedTest {
        public com.yzxie.easy.log.web.SynchronizedTest();
          Code:
             0: aload_0
             1: invokespecial #1                  // Method java/lang/Object."<init>":()V
             4: return
      
        public void method();
          Code:
             0: aload_0
             1: dup
             2: astore_1
             3: monitorenter
             4: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
             7: ldc           #3                  // String Hello world
             9: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
            12: aload_1
            13: monitorexit
            14: goto          22
            17: astore_2
            18: aload_1
            19: monitorexit
            20: aload_2
            21: athrow
            22: return
          Exception table:
             from    to  target type
                 4    14    17   any
                17    20    17   any
      }
      
  • 关于monitorenter和monitorexit的工作原理,先看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.
    
    • 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的作用:所有线程共享该同步代码和该对象关联的监视器monitor,每个线程执行到monitorenter指令的时候,会检查对应的monitor对象的计数是否为0,是则当前线程成为该monitor对象的owner,即锁住该monitor对象了,并递增该计数为1,之后该线程每调用一次使用了该monitor对象进行同步的方法,计数加一(所以synchronized也是可重入的);其他线程检查到monitor对象的计数不为0,则知道该monitor对象已经被其他线程持有锁住了,故当前线程会阻塞直到该monitor的计数重新变为0,则阻塞的线程们会继续竞争成为该monitor的owner,从而可以访问同步代码。

  • monitorexit的作用:当持有该monitor对象的线程每执行完一个同步代码时(如对于成员方法,如果该线程调用了多个使用sychronized修饰的成员方法,则每个方法执行完执行一次monitor减一),将monitor的计数减一,当monitor对象的计数递减到0时,则当前线程不再持有该monitor对象,其他阻塞的线程此时可以竞争成为该monitor的owner,成功的线程可以访问同步代码。

  • 为什么monitor对象的wait,notify,notifyAll需要在synchronized同步代码里面使用呢?首先需要理解以下概念:

    1. 每个对象关联一个监视器monitor;
    2. 每个监视器monitor都有一个该对象的锁(即计数是否为0,为0则说明没有其他线程加锁),一个等待队列和一个同步队列;
    • wait方法:释放对象锁,然后进入等待队列;
    • notify和notifyAll方法:从等待队列被唤醒,放到同步队列去竞争该对象锁;
    • 所以线程在执行wait,notify,notifyAll时需要依赖该监视器monitor,即该线程成为该监视器的owner,从而可以访问synchronized包围的同步代码,这样才能有权访问该监视器对应的对象锁,等待队列和同步队列。
操作系统层面
  • 在操作系统层面,synchronized是基于操作系统的Metux Lock来实现的,而操作系统实现线程之间的切换是需要进行上下文切换的,即从用户态切换到内核态,所以这也是synchronized相对来说成本较高,性能相对较低的原因。

猜你喜欢

转载自blog.csdn.net/u010013573/article/details/87219343