1 synchronized应用场景

1.1 synchronized代码块

  1. public class SynObj {
  2. public synchronized void methodA() {
  3. System.out.println("methodA.....");
  4. try {
  5. Thread.sleep(5000);
  6. } catch (InterruptedException e) {
  7. e.printStackTrace();
  8. }
  9. }
  10. public void methodB() {
  11. synchronized(this){
  12. System.out.println("methodB.....");
  13. }
  14. }
  15. public void methodC() {
  16. String str = "sss";
  17. synchronized (str) {
  18. System.out.println("methodC.....");
  19. }
  20. }
  21. public synchronized static void methodD() {
  22. System.out.println("methodD.....");
  23. try {
  24. Thread.sleep(5000);
  25. } catch (InterruptedException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. public void methodE() {
  30. synchronized(this){
  31. System.out.println("methodE.....");
  32. }
  33. }
  34. public static void main(String[] args) {
  35. final SynObj obj = new SynObj();
  36. Thread t1 = new Thread(new Runnable() {
  37. @Override
  38. public void run() {
  39. obj.methodA();
  40. }
  41. });
  42. t1.start();
  43. Thread t2 = new Thread(new Runnable() {
  44. @Override
  45. public void run() {
  46. obj.methodB();
  47. }
  48. });
  49. t2.start();
  50. Thread t3 = new Thread(new Runnable() {
  51. @Override
  52. public void run() {
  53. obj.methodC();
  54. }
  55. });
  56. t3.start();
  57. Thread t4 = new Thread(new Runnable() {
  58. @Override
  59. public void run() {
  60. SynObj.methodD();
  61. }
  62. });
  63. t4.start();
  64. Thread t5 = new Thread(new Runnable() {
  65. @Override
  66. public void run() {
  67. obj.methodE();
  68. }
  69. });
  70. t5.start();
  71. }
  72. }

从结果可以看出,synchronized(this)以及非static的synchronized方法,只能防止多个线程同时执行同一个对象的同步代码段。synchronized锁住的是括号里的对象,而不是代码。对于非static的synchronized方法,锁的就是对象本身也就是this。

当synchronized锁住一个对象后,别的线程如果也想拿到这个对象的锁,就必须等待这个线程执行完成释放锁,才能再次给对象加锁,这样才达到线程同步的目的。即使两个不同的代码段,都要锁同一个对象,那么这两个代码段也不能在多线程环境下同时运行。

所以我们在用synchronized关键字的时候,能缩小代码段的范围就尽量缩小,能在代码段上加同步就不要再整个方法上加同步。这叫减小锁的粒度,使代码更大程度的并发。


synchronized(this)锁定该对象,和非static的synchronized方法一样,而synchronized(Sync.class)实现了全局锁的效果,同static synchronized方法一样。

1.2 synchronized方法

synchronized是对类的当前实例(当前对象)进行加锁,防止其他线程同时访问该类的该实例的所有synchronized块,注意这里是“类的当前实例”, 类的两个不同实例就没有这种约束了。
static synchronized是要控制类的所有实例的并发访问,static synchronized是限制多线程中该类的所有实例同时访问jvm中该类所对应的代码块。实际上,在类中如果某方法或某代码块中有 synchronized,那么在生成一个该类实例后,该实例也就有一个监视块,防止线程并发访问该实例的synchronized保护块,而static synchronized则是所有该类的所有实例公用得一个监视块,这就是他们两个的区别。也就是说synchronized相当于 this.synchronized,而static synchronized相当于Something.synchronized。
下面看一个例子:

Java 虚拟机中的同步(Synchronization)基于进入和退出管程(Monitor)对象实现, 无论是显式同步(有明确的 monitorenter 和monitorexit指令,即同步代码块)还是隐式同步都是如此。在 Java 语言中,同步用的最多的地方可能是被synchronized修饰的同步方法。同步方法并不是由monitorenter和monitorexit指令来实现同步的,而是由方法调用指令读取运行时常量池中方法的ACC_SYNCHRONIZED标志来隐式实现的,关于这点,稍后详细分析。下面先来了解一个概念Java对象头,这对深入理解synchronized实现原理非常关键。

2.1 Java对象头

HotSpot虚拟机中,对象在内存中存储的布局可以分为三块区域:对象头(Header)、实例数据(Instance Data)和对齐填充(Padding)。
image.png
对于Java头对象,它实现synchronized的锁对象的基础,这点我们重点分析它,一般而言,synchronized使用的锁对象是存储在Java对象头里的,jvm中采用2个字来存储对象头(如果对象是数组则会分配3个字,多出来的1个字记录的是数组长度),其主要结构是由Mark Word 和 Class Metadata Address 组成,其结构说明如下表:
image.png
HotSpot虚拟机的对象头(Object Header)包括两部分信息,第一部分用于存储对象自身的运行时数据, 如哈希码(HashCode)、GC分代年龄、锁状态标志、线程持有的锁、偏向线程ID、偏向时间戳等等,这部分数据的长度在32位和64位的虚拟机(暂不考虑开启压缩指针的场景)中分别为32个和64个Bits,官方称它为“Mark Word”。另外一部分是类型指针,即是对象指向它的类的元数据的指针,虚拟机通过这个指针来确定这个对象是哪个类的实例。并不是所有的虚拟机实现都必须在对象数据上保留类型指针,换句话说查找对象的元数据信息并不一定要经过对象本身。另外,如果对象是一个Java数组,那在对象头中还必须有一块用于记录数组长度的数据,因为虚拟机可以通过普通Java对象的元数据信息确定Java对象的大小,但是从数组的元数据中无法确定数组的大小。
对于第一部分,Mark Word在默认情况下存储着对象的HashCode、分代年龄、锁标记位等以下是32位JVM的Mark Word默认存储结构:

锁状态 25bit 4bit 1bit是否是偏向锁 2bit锁标志位
无锁状态 对象HashCode 对象分代年龄 0 01

由于对象头的信息是与对象自身定义的数据没有关系的额外存储成本,因此考虑到JVM的空间效率,Mark Word 被设计成为一个非固定的数据结构,以便存储更多有效的数据,它会根据对象本身的状态复用自己的存储空间,如32位JVM下,除了上述列出的Mark Word默认存储结构外,还有如下可能变化的结构:
image.png
其中轻量级锁和偏向锁是Java6对synchronized锁进行优化后新增加的,稍后我们会简要分析。

2.2 Monitor对象

  1. 重量级锁通过对象内部的监视器(monitor)实现,其中monitor的本质是依赖于底层操作系统的Mutex Lock实现,操作系统实现线程之间的切换需要从用户态到内核态的切换,切换成本非常高。<br />当锁标识位为10,其中指针指向的是monitor对象(也称为管程或监视器锁)的起始地址。每个对象都存在着一个 monitor 与之关联,对象与其 monitor 之间的关系有存在多种实现方式,如monitor可以与对象一起创建销毁或当线程试图获取对象锁时自动生成,但当一个monitor被某个线程持有后,它便处于锁定状态。在Java虚拟机(HotSpot)中,monitor是由ObjectMonitor实现的,其主要数据结构如下(位于HotSpot虚拟机源码ObjectMonitor.hpp文件,C++实现的)
  1. ObjectMonitor() {
  2. _header = NULL;
  3. _count = 0; //用来记录该线程获取锁的次数
  4. _waiters = 0,
  5. _recursions = 0; //锁的重入次数
  6. _object = NULL;
  7. _owner = NULL; //当前拥有锁的线程
  8. _WaitSet = NULL; //调用了wait()方法的线程,会被加入到_WaitSet
  9. _WaitSetLock = 0 ;
  10. _Responsible = NULL ;
  11. _succ = NULL ;
  12. _cxq = NULL ;//处于等待锁被挂起的线程列表,JDK8默认策略下,是一个后进先出(LIFO)的队列,每次放入和取出都操作队头
  13. FreeNext = NULL ;
  14. _EntryList = NULL ; //处于等待锁挂起状态的线程,有资格成为候选的线程会被加入到该列表
  15. _SpinFreq = 0 ;
  16. _SpinClock = 0 ;
  17. OwnerIsThread = 0 ;
  18. }
  1. 由以上ObjectMonitor的构造函数可以看出,ObjectMonitor中有三个重要队列,_cxq_WaitSet _EntryList,用来保存ObjectWaiter对象列表。ObjectWaiter 对象里存放的就是thread(线程对象), 每一个等待锁的线程都被封装成一个ObjectWaiter对象,ObjectWaiter是一个双向链表结构的对象。_owner指向持有ObjectMonitor对象的线程,也就是当前拥有锁的线程。下图展示了JDK8默认设置下(Policy2QMode0)竞争重量级锁的线程的流转过程。<br />若持有monitor的线程调用wait()方法,将释放当前持有的monitor_owner变量恢复为null_count自减1,同时该线程进入_WaitSet集合中等待被唤醒。若当前线程执行完毕也将释放monitor(锁)并复位变量的值,以便其他线程进入获取monitor(锁)。如下图所示

image.png
下图展示了JDK8默认设置下(Policy为2,QMode为0)竞争重量级锁的线程的流转过程。

image.png

2.3 synchronized锁介绍

大家都知道java中锁synchronized性能较差,线程会阻塞。但是在jdk1.6中对锁的实现引入了大量的优化来减少锁操作的开销:
锁粗化(Lock Coarsening):将多个连续的锁扩展成一个范围更大的锁,用以减少频繁互斥同步导致的性能损耗。
锁消除(Lock Elimination):JVM及时编译器在运行时,通过逃逸分析,如果判断一段代码中,堆上的所有数据不会逃逸出去从来被其他线程访问到,就可以去除这些锁。
轻量级锁(Lightweight Locking):JDK1.6引入。在没有多线程竞争的情况下避免重量级互斥锁,只需要依靠一条CAS原子指令就可以完成锁的获取及释放。
偏向锁(Biased Locking):JDK1.6引入。目的是消除数据再无竞争情况下的同步原语。使用CAS记录获取它的线程。下一次同一个线程进入则偏向该线程,无需任何同步操作。
适应性自旋(Adaptive Spinning):为了避免线程频繁挂起、恢复的状态切换消耗。产生了忙循环(循环时间固定),即自旋。JDK1.6引入了自适应自旋。自旋时间根据之前锁自旋时间和线程状态,动态变化,用以期望能减少阻塞的时间。
锁升级:偏向锁—》轻量级锁—》重量级锁
从左往右可以升级,从右往左不能降级

2.3.1 偏向锁

引入偏向锁的目的:在没有多线程竞争的情况下,尽量减少不必要的轻量级锁执行路径,轻量级锁的获取及释放依赖多次CAS原子指令,而偏向锁只依赖一次CAS原子指令置换ThreadID,不过一旦出现多个线程竞争时必须撤销偏向锁,所以撤销偏向锁消耗的性能必须小于之前节省下来的CAS原子操作的性能消耗,不然就得不偿失了。JDK1.6中默认开启偏向锁,可以通过-XX:-UseBiasedLocking来禁用偏向锁。
因为大多数情况下,锁不仅不存在多线程竞争,而且总是由统一线程多次获得,为了让线程获得锁的代价更低而引入了偏向锁。当一个线程访问同步代码块并获取锁时,会在对象头和栈帧中的锁记录里存储锁偏向的线程ID,以后该线程在进入和退出同步代码块时不需要进行CAS操作来加锁和解锁,只需简单地测试一下对象头的Mark Work里是否存储着指向当前线程的偏向锁。
下面看一下偏向锁的整体流程:
image.png
以同步代码块为例,在JVM被编译为monitorenter、monitorexit指令来获取和释放互斥锁。
解释器执行monitorenter时会进入到InterpreterRuntime.cpp的InterpreterRuntime::monitorenter函数,具体实现如下:

  1. IRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* thread, BasicObjectLock* elem))
  2. #ifdef ASSERT
  3. thread->last_frame().interpreter_frame_verify_monitor(elem);
  4. #endif
  5. if (PrintBiasedLockingStatistics) {
  6. Atomic::inc(BiasedLocking::slow_path_entry_count_addr());
  7. }
  8. Handle h_obj(thread, elem->obj());
  9. assert(Universe::heap()->is_in_reserved_or_null(h_obj()),
  10. "must be NULL or an object");
  11. if (UseBiasedLocking) {//标识虚拟机是否开启偏向锁功能,默认开启
  12. // Retry fast entry if bias is revoked to avoid unnecessary inflation
  13. ObjectSynchronizer::fast_enter(h_obj, elem->lock(), true, CHECK);
  14. } else {
  15. ObjectSynchronizer::slow_enter(h_obj, elem->lock(), CHECK);
  16. }
  17. assert(Universe::heap()->is_in_reserved_or_null(elem->obj()),
  18. "must be NULL or an object");
  19. #ifdef ASSERT
  20. thread->last_frame().interpreter_frame_verify_monitor(elem);
  21. #endif
  22. IRT_END
  1. 其中,参数JavaThreadthread指向java中的当前线程;参数BasicObjectLock类型的elem对象包含一个BasicLock类型_lock对象和一个指向Object对象的指针_objBasicLock类型_lock对象主要用来保存_obj指向Object对象的对象头数据;UseBiasedLocking标识虚拟机是否开启偏向锁功能,如果开启则执行fast_enter逻辑,否则执行slow_enter
  1. void ObjectSynchronizer::fast_enter(Handle obj, BasicLock* lock, bool attempt_rebias, TRAPS) {
  2. if (UseBiasedLocking) {
  3. if (!SafepointSynchronize::is_at_safepoint()) {
  4. BiasedLocking::Condition cond = BiasedLocking::revoke_and_rebias(obj, attempt_rebias, THREAD);
  5. if (cond == BiasedLocking::BIAS_REVOKED_AND_REBIASED) {
  6. return;
  7. }
  8. } else {
  9. assert(!attempt_rebias, "can not rebias toward VM thread");
  10. BiasedLocking::revoke_at_safepoint(obj);
  11. }
  12. assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
  13. }
  14. //轻量级锁
  15. slow_enter (obj, lock, THREAD) ;
  16. }

2.3.1.1 偏向锁的获取

偏向锁的获取由BiasedLocking::revoke_and_rebias方法实现,实现逻辑如下:
1、通过markOop mark=obj->mark()获取对象的markOop数据mark,即对象头的Mark Word;
2、判断mark是否为可偏向状态,即mark的偏向锁标志位为 1,锁标志位为 01;
3、判断mark中JavaThread的状态:如果为空,则进入步骤(4);如果指向当前线程,则执行同步代码块;如果指向其它线程,进入步骤(5);
4、通过CAS原子指令设置mark中JavaThread为当前线程ID,如果执行CAS成功,则执行同步代码块,否则进入步骤(5);
5、如果执行CAS失败,表示当前存在多个线程竞争锁,当达到全局安全点(safepoint),获得偏向锁的线程被挂起,撤销偏向锁,并升级为轻量级,升级完成后被阻塞在安全点的线程继续执行同步代码块;

2.3.1.2 偏向锁的撤销

只有当其它线程尝试竞争偏向锁时,持有偏向锁的线程才会释放锁,偏向锁的撤销由BiasedLocking::revoke_at_safepoint方法实现:

  1. void BiasedLocking::revoke_at_safepoint(Handle h_obj) {
  2. assert(SafepointSynchronize::is_at_safepoint(), "must only be called while at safepoint");//校验全局安全点
  3. oop obj = h_obj();
  4. HeuristicsResult heuristics = update_heuristics(obj, false);
  5. if (heuristics == HR_SINGLE_REVOKE) {
  6. revoke_bias(obj, false, false, NULL);
  7. } else if ((heuristics == HR_BULK_REBIAS) ||
  8. (heuristics == HR_BULK_REVOKE)) {
  9. bulk_revoke_or_rebias_at_safepoint(obj, (heuristics == HR_BULK_REBIAS), false, NULL);
  10. }
  11. clean_up_cached_monitor_info();
  12. }

2.3.2 轻量级锁

引入轻量级锁的目的:在多线程交替执行同步块的情况下,尽量避免重量级锁引起的性能消耗,但是如果多个线程在同一时刻进入临界区,会导致轻量级锁膨胀升级重量级锁,所以轻量级锁的出现并非是要替代重量级锁。
因为轻量级锁是通过自旋来获取锁,但是自旋会消耗CPU,为了避免无用的自旋(比如获得锁的线程被阻塞了),此时会将轻量级锁升级为重量级锁,并且不会再回到轻量级锁。当处于重量级锁的情形下,其他线程试图获取锁时,都被被阻塞,当持有锁的线程释放锁之后会唤醒这些线程,被唤醒的线程就会进入新一轮的竞争锁。

2.3.2.1 轻量级锁的获取

当关闭偏向锁功能,或多个线程竞争偏向锁导致偏向锁升级为轻量级锁,会尝试获取轻量级锁,其入口位于ObjectSynchronizer::slow_enter

  1. void ObjectSynchronizer::slow_enter(Handle obj, BasicLock* lock, TRAPS) {
  2. markOop mark = obj->mark();
  3. assert(!mark->has_bias_pattern(), "should not see bias pattern here");
  4. if (mark->is_neutral()) {//是否为无锁状态001
  5. // Anticipate successful CAS -- the ST of the displaced mark must
  6. // be visible <= the ST performed by the CAS.
  7. lock->set_displaced_header(mark);
  8. if (mark == (markOop) Atomic::cmpxchg_ptr(lock, obj()->mark_addr(), mark)) {//CAS成功,释放栈锁
  9. TEVENT (slow_enter: release stacklock) ;
  10. return ;
  11. }
  12. // Fall through to inflate() ...
  13. } else
  14. if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) {
  15. assert(lock != mark->locker(), "must not re-lock the same lock");
  16. assert(lock != (BasicLock*)obj->mark(), "don't relock with same BasicLock");
  17. lock->set_displaced_header(NULL);
  18. return;
  19. }
  20. #if 0
  21. // The following optimization isn't particularly useful.
  22. if (mark->has_monitor() && mark->monitor()->is_entered(THREAD)) {
  23. lock->set_displaced_header (NULL) ;
  24. return ;
  25. }
  26. #endif
  27. // The object header will never be displaced to this lock,
  28. // so it does not matter what the value is, except that it
  29. // must be non-zero to avoid looking like a re-entrant lock,
  30. // and must not look locked either.
  31. lock->set_displaced_header(markOopDesc::unused_mark());
  32. ObjectSynchronizer::inflate(THREAD, obj())->enter(THREAD);
  33. }

1、markOop mark = obj->mark()方法获取对象的markOop数据mark;
2、mark->is_neutral()方法判断mark是否为无锁状态:mark的偏向锁标志位为 0,锁标志位为 01;
3、如果mark处于无锁状态,则进入步骤(4),否则执行步骤(6);
4、把mark保存到BasicLock对象的_displaced_header字段;
5、通过CAS尝试将Mark Word更新为指向BasicLock对象的指针,如果更新成功,表示竞争到锁,则执行同步代码,否则执行步骤(6);
6、如果当前mark处于加锁状态,且mark中的ptr指针指向当前线程的栈帧,则执行同步代码,否则说明有多个线程竞争轻量级锁,轻量级锁需要膨胀升级为重量级锁;
假设线程A和B同时执行到临界区if (mark->is_neutral()):
1、线程AB都把Mark Word复制到各自的_displaced_header字段,该数据保存在线程的栈帧上,是线程私有的;
2、Atomic::cmpxchg_ptr原子操作保证只有一个线程可以把指向栈帧的指针复制到Mark Word,假设此时线程A执行成功,并返回继续执行同步代码块;
3、线程B执行失败,退出临界区,通过ObjectSynchronizer::inflate方法开始膨胀锁;
注意,如果轻量级锁膨胀成重量级锁后,才会开始竞争锁,调用ObjectSynchronizer::inflate(THREAD, obj())->enter(THREAD)竞争锁。

2.3.2.2 轻量级锁释放

轻量级锁的释放通过ObjectSynchronizer::fast_exit完成。

  1. void ObjectSynchronizer::fast_exit(oop object, BasicLock* lock, TRAPS) {
  2. assert(!object->mark()->has_bias_pattern(), "should not see bias pattern here");
  3. // if displaced header is null, the previous enter is recursive enter, no-op
  4. markOop dhw = lock->displaced_header();
  5. markOop mark ;
  6. if (dhw == NULL) {
  7. // Recursive stack-lock.
  8. // Diagnostics -- Could be: stack-locked, inflating, inflated.
  9. mark = object->mark() ;
  10. assert (!mark->is_neutral(), "invariant") ;
  11. if (mark->has_locker() && mark != markOopDesc::INFLATING()) {
  12. assert(THREAD->is_lock_owned((address)mark->locker()), "invariant") ;
  13. }
  14. if (mark->has_monitor()) {
  15. ObjectMonitor * m = mark->monitor() ;
  16. assert(((oop)(m->object()))->mark() == mark, "invariant") ;
  17. assert(m->is_entered(THREAD), "invariant") ;
  18. }
  19. return ;
  20. }
  21. mark = object->mark() ;
  22. // If the object is stack-locked by the current thread, try to
  23. // swing the displaced header from the box back to the mark.
  24. if (mark == (markOop) lock) {
  25. assert (dhw->is_neutral(), "invariant") ;
  26. if ((markOop) Atomic::cmpxchg_ptr (dhw, object->mark_addr(), mark) == mark) {//成功的释放了锁
  27. TEVENT (fast_exit: release stacklock) ;
  28. return;
  29. }
  30. }
  31. ObjectSynchronizer::inflate(THREAD, object)->exit (true, THREAD) ;//锁膨胀升级
  32. }

1、确保处于偏向锁状态时不会执行这段逻辑;
2、取出在获取轻量级锁时保存在BasicLock对象的mark数据dhw;
3、通过CAS尝试把dhw替换到当前的Mark Word,如果CAS成功,说明成功的释放了锁,否则执行步骤(4);
4、如果CAS失败,说明有其它线程在尝试获取该锁,这时需要将该锁升级为重量级锁,并释放;

2.3.3 重量级锁

2.3.3.1 锁膨胀过程

锁的膨胀过程通过ObjectSynchronizer::inflate函数实现

  1. ObjectMonitor * ATTR ObjectSynchronizer::inflate (Thread * Self, oop object) {
  2. // Inflate mutates the heap ...
  3. // Relaxing assertion for bug 6320749.
  4. assert (Universe::verify_in_progress() ||
  5. !SafepointSynchronize::is_at_safepoint(), "invariant") ;
  6. for (;;) {//自旋
  7. const markOop mark = object->mark() ;
  8. assert (!mark->has_bias_pattern(), "invariant") ;
  9. // The mark can be in one of the following states:
  10. // * Inflated - just return
  11. // * Stack-locked - coerce it to inflated
  12. // * INFLATING - busy wait for conversion to complete
  13. // * Neutral - aggressively inflate the object.
  14. // * BIASED - Illegal. We should never see this
  15. // CASE: inflated已膨胀,即重量级锁
  16. if (mark->has_monitor()) {//判断当前是否为重量级锁
  17. ObjectMonitor * inf = mark->monitor() ;//获取指向ObjectMonitor的指针
  18. assert (inf->header()->is_neutral(), "invariant");
  19. assert (inf->object() == object, "invariant") ;
  20. assert (ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid");
  21. return inf ;
  22. }
  23. // CASE: inflation in progress - inflating over a stack-lock.膨胀等待(其他线程正在从轻量级锁转为膨胀锁)
  24. // Some other thread is converting from stack-locked to inflated.
  25. // Only that thread can complete inflation -- other threads must wait.
  26. // The INFLATING value is transient.
  27. // Currently, we spin/yield/park and poll the markword, waiting for inflation to finish.
  28. // We could always eliminate polling by parking the thread on some auxiliary list.
  29. if (mark == markOopDesc::INFLATING()) {
  30. TEVENT (Inflate: spin while INFLATING) ;
  31. ReadStableMark(object) ;
  32. continue ;
  33. }
  34. // CASE: stack-locked栈锁(轻量级锁)
  35. // Could be stack-locked either by this thread or by some other thread.
  36. //
  37. // Note that we allocate the objectmonitor speculatively, _before_ attempting
  38. // to install INFLATING into the mark word. We originally installed INFLATING,
  39. // allocated the objectmonitor, and then finally STed the address of the
  40. // objectmonitor into the mark. This was correct, but artificially lengthened
  41. // the interval in which INFLATED appeared in the mark, thus increasing
  42. // the odds of inflation contention.
  43. //
  44. // We now use per-thread private objectmonitor free lists.
  45. // These list are reprovisioned from the global free list outside the
  46. // critical INFLATING...ST interval. A thread can transfer
  47. // multiple objectmonitors en-mass from the global free list to its local free list.
  48. // This reduces coherency traffic and lock contention on the global free list.
  49. // Using such local free lists, it doesn't matter if the omAlloc() call appears
  50. // before or after the CAS(INFLATING) operation.
  51. // See the comments in omAlloc().
  52. if (mark->has_locker()) {
  53. ObjectMonitor * m = omAlloc (Self) ;//获取一个可用的ObjectMonitor
  54. // Optimistically prepare the objectmonitor - anticipate successful CAS
  55. // We do this before the CAS in order to minimize the length of time
  56. // in which INFLATING appears in the mark.
  57. m->Recycle();
  58. m->_Responsible = NULL ;
  59. m->OwnerIsThread = 0 ;
  60. m->_recursions = 0 ;
  61. m->_SpinDuration = ObjectMonitor::Knob_SpinLimit ; // Consider: maintain by type/class
  62. markOop cmp = (markOop) Atomic::cmpxchg_ptr (markOopDesc::INFLATING(), object->mark_addr(), mark) ;
  63. if (cmp != mark) {//CAS失败//CAS失败,说明冲突了,自旋等待//CAS失败,说明冲突了,自旋等待//CAS失败,说明冲突了,自旋等待
  64. omRelease (Self, m, true) ;//释放监视器锁
  65. continue ; // Interference -- just retry
  66. }
  67. // We've successfully installed INFLATING (0) into the mark-word.
  68. // This is the only case where 0 will appear in a mark-work.
  69. // Only the singular thread that successfully swings the mark-word
  70. // to 0 can perform (or more precisely, complete) inflation.
  71. //
  72. // Why do we CAS a 0 into the mark-word instead of just CASing the
  73. // mark-word from the stack-locked value directly to the new inflated state?
  74. // Consider what happens when a thread unlocks a stack-locked object.
  75. // It attempts to use CAS to swing the displaced header value from the
  76. // on-stack basiclock back into the object header. Recall also that the
  77. // header value (hashcode, etc) can reside in (a) the object header, or
  78. // (b) a displaced header associated with the stack-lock, or (c) a displaced
  79. // header in an objectMonitor. The inflate() routine must copy the header
  80. // value from the basiclock on the owner's stack to the objectMonitor, all
  81. // the while preserving the hashCode stability invariants. If the owner
  82. // decides to release the lock while the value is 0, the unlock will fail
  83. // and control will eventually pass from slow_exit() to inflate. The owner
  84. // will then spin, waiting for the 0 value to disappear. Put another way,
  85. // the 0 causes the owner to stall if the owner happens to try to
  86. // drop the lock (restoring the header from the basiclock to the object)
  87. // while inflation is in-progress. This protocol avoids races that might
  88. // would otherwise permit hashCode values to change or "flicker" for an object.
  89. // Critically, while object->mark is 0 mark->displaced_mark_helper() is stable.
  90. // 0 serves as a "BUSY" inflate-in-progress indicator.
  91. // fetch the displaced mark from the owner's stack.
  92. // The owner can't die or unwind past the lock while our INFLATING
  93. // object is in the mark. Furthermore the owner can't complete
  94. // an unlock on the object, either.
  95. markOop dmw = mark->displaced_mark_helper() ;
  96. assert (dmw->is_neutral(), "invariant") ;
  97. //CAS成功,设置ObjectMonitor的_header、_owner和_object等
  98. // Setup monitor fields to proper values -- prepare the monitor
  99. m->set_header(dmw) ;
  100. // Optimization: if the mark->locker stack address is associated
  101. // with this thread we could simply set m->_owner = Self and
  102. // m->OwnerIsThread = 1. Note that a thread can inflate an object
  103. // that it has stack-locked -- as might happen in wait() -- directly
  104. // with CAS. That is, we can avoid the xchg-NULL .... ST idiom.
  105. m->set_owner(mark->locker());
  106. m->set_object(object);
  107. // TODO-FIXME: assert BasicLock->dhw != 0.
  108. // Must preserve store ordering. The monitor state must
  109. // be stable at the time of publishing the monitor address.
  110. guarantee (object->mark() == markOopDesc::INFLATING(), "invariant") ;
  111. object->release_set_mark(markOopDesc::encode(m));
  112. // Hopefully the performance counters are allocated on distinct cache lines
  113. // to avoid false sharing on MP systems ...
  114. if (ObjectMonitor::_sync_Inflations != NULL) ObjectMonitor::_sync_Inflations->inc() ;
  115. TEVENT(Inflate: overwrite stacklock) ;
  116. if (TraceMonitorInflation) {
  117. if (object->is_instance()) {
  118. ResourceMark rm;
  119. tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
  120. (void *) object, (intptr_t) object->mark(),
  121. object->klass()->external_name());
  122. }
  123. }
  124. return m ;
  125. }
  126. // CASE: neutral 无锁
  127. // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
  128. // If we know we're inflating for entry it's better to inflate by swinging a
  129. // pre-locked objectMonitor pointer into the object header. A successful
  130. // CAS inflates the object *and* confers ownership to the inflating thread.
  131. // In the current implementation we use a 2-step mechanism where we CAS()
  132. // to inflate and then CAS() again to try to swing _owner from NULL to Self.
  133. // An inflateTry() method that we could call from fast_enter() and slow_enter()
  134. // would be useful.
  135. assert (mark->is_neutral(), "invariant");
  136. ObjectMonitor * m = omAlloc (Self) ;
  137. // prepare m for installation - set monitor to initial state
  138. m->Recycle();
  139. m->set_header(mark);
  140. m->set_owner(NULL);
  141. m->set_object(object);
  142. m->OwnerIsThread = 1 ;
  143. m->_recursions = 0 ;
  144. m->_Responsible = NULL ;
  145. m->_SpinDuration = ObjectMonitor::Knob_SpinLimit ; // consider: keep metastats by type/class
  146. if (Atomic::cmpxchg_ptr (markOopDesc::encode(m), object->mark_addr(), mark) != mark) {
  147. m->set_object (NULL) ;
  148. m->set_owner (NULL) ;
  149. m->OwnerIsThread = 0 ;
  150. m->Recycle() ;
  151. omRelease (Self, m, true) ;
  152. m = NULL ;
  153. continue ;
  154. // interference - the markword changed - just retry.
  155. // The state-transitions are one-way, so there's no chance of
  156. // live-lock -- "Inflated" is an absorbing state.
  157. }
  158. // Hopefully the performance counters are allocated on distinct
  159. // cache lines to avoid false sharing on MP systems ...
  160. if (ObjectMonitor::_sync_Inflations != NULL) ObjectMonitor::_sync_Inflations->inc() ;
  161. TEVENT(Inflate: overwrite neutral) ;
  162. if (TraceMonitorInflation) {
  163. if (object->is_instance()) {
  164. ResourceMark rm;
  165. tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
  166. (void *) object, (intptr_t) object->mark(),
  167. object->klass()->external_name());
  168. }
  169. }
  170. return m ;
  171. }
  172. }

膨胀过程的实现比较复杂,大概实现过程如下:
1、整个膨胀过程在自旋下完成;
2、mark->has_monitor()方法判断当前是否为重量级锁(上图18-25行),即Mark Word的锁标识位为 10,如果当前状态为重量级锁,执行步骤(3),否则执行步骤(4);
3、mark->monitor()方法获取指向ObjectMonitor的指针,并返回,说明膨胀过程已经完成;
4、如果当前锁处于膨胀中(上图33-37行),说明该锁正在被其它线程执行膨胀操作,则当前线程就进行自旋等待锁膨胀完成,这里需要注意一点,虽然是自旋操作,但不会一直占用cpu资源,每隔一段时间会通过os::NakedYield方法放弃cpu资源,或通过park方法挂起;如果其他线程完成锁的膨胀操作,则退出自旋并返回;
5、如果当前是轻量级锁状态(上图58-138行),即锁标识位为 00,膨胀过程如下:
通过omAlloc方法,获取一个可用的ObjectMonitor monitor,并重置monitor数据;
通过CAS尝试将Mark Word设置为markOopDesc:INFLATING,标识当前锁正在膨胀中,如果CAS失败,说明同一时刻其它线程已经将Mark Word设置为markOopDesc:INFLATING,当前线程进行自旋等待膨胀完成;
如果CAS成功,设置monitor的各个字段:_header、_owner和_object等,并返回;
6、如果是无锁(中立,上图150-186行),重置监视器值;

2.3.3.2 monitor竞争

当锁膨胀完成并返回对应的monitor时,并不表示该线程竞争到了锁,真正的锁竞争发生在ObjectMonitor::enter方法中。
ObjectMonitor类中提供了几个方法:
获取锁:

  1. void ATTR ObjectMonitor::enter(TRAPS) {
  2. Thread * const Self = THREAD ;
  3. void * cur ;
  4. //通过CAS尝试把monitor的`_owner`字段设置为当前线程
  5. cur = Atomic::cmpxchg_ptr (Self, &_owner, NULL) ;
  6. //获取锁失败
  7. if (cur == NULL) {
  8. assert (_recursions == 0 , "invariant") ;
  9. assert (_owner == Self, "invariant") ;
  10. // CONSIDER: set or assert OwnerIsThread == 1
  11. return ;
  12. }
  13. // 如果旧值和当前线程一样,说明当前线程已经持有锁,此次为重入,_recursions自增,并获得锁。
  14. if (cur == Self) {
  15. // TODO-FIXME: check for integer overflow! BUGID 6557169.
  16. _recursions ++ ;
  17. return ;
  18. }
  19. // 如果当前线程是第一次进入该monitor,设置_recursions为1,_owner为当前线程
  20. if (Self->is_lock_owned ((address)cur)) {
  21. assert (_recursions == 0, "internal state error");
  22. _recursions = 1 ;
  23. // Commute owner from a thread-specific on-stack BasicLockObject address to
  24. // a full-fledged "Thread *".
  25. _owner = Self ;
  26. OwnerIsThread = 1 ;
  27. return ;
  28. }
  29. // 省略部分代码。
  30. // 通过自旋执行ObjectMonitor::EnterI方法等待锁的释放
  31. for (;;) {
  32. jt->set_suspend_equivalent();
  33. // cleared by handle_special_suspend_equivalent_condition()
  34. // or java_suspend_self()
  35. EnterI (THREAD) ;
  36. if (!ExitSuspendEquivalent(jt)) break ;
  37. //
  38. // We have acquired the contended monitor, but while we were
  39. // waiting another thread suspended us. We don't want to enter
  40. // the monitor while suspended because that would surprise the
  41. // thread that suspended us.
  42. //
  43. _recursions = 0 ;
  44. _succ = NULL ;
  45. exit (Self) ;
  46. jt->java_suspend_self();
  47. }
  48. }

1、通过CAS尝试把monitor的_owner字段设置为当前线程;
2、如果设置之前的_owner指向当前线程,说明当前线程再次进入monitor,即重入锁,执行_recursions ++ ,记录重入的次数;
3、如果之前的_owner指向的地址在当前线程中,这种描述有点拗口,换一种说法:之前_owner指向的BasicLock在当前线程栈上,说明当前线程是第一次进入该monitor,设置_recursions为1,_owner为当前线程,该线程成功获得锁并返回;
4、如果获取锁失败,则等待锁的释放;

image.png

2.3.3.3 monitor等待

monitor竞争失败的线程,通过自旋执行ObjectMonitor::EnterI方法等待锁的释放,EnterI方法的部分逻辑实现如下:

  1. ObjectWaiter node(Self) ;
  2. Self->_ParkEvent->reset() ;
  3. node._prev = (ObjectWaiter *) 0xBAD ;
  4. node.TState = ObjectWaiter::TS_CXQ ;
  5. // Push "Self" onto the front of the _cxq.
  6. // Once on cxq/EntryList, Self stays on-queue until it acquires the lock.
  7. // Note that spinning tends to reduce the rate at which threads
  8. // enqueue and dequeue on EntryList|cxq.
  9. ObjectWaiter * nxt ;
  10. for (;;) {
  11. node._next = nxt = _cxq ;
  12. if (Atomic::cmpxchg_ptr (&node, &_cxq, nxt) == nxt) break ;
  13. // Interference - the CAS failed because _cxq changed. Just retry.
  14. // As an optional optimization we retry the lock.
  15. if (TryLock (Self) > 0) {
  16. assert (_succ != Self , "invariant") ;
  17. assert (_owner == Self , "invariant") ;
  18. assert (_Responsible != Self , "invariant") ;
  19. return ;
  20. }
  21. }

1、当前线程被封装成ObjectWaiter对象node,状态设置成ObjectWaiter::TS_CXQ;
2、在for循环中,通过CAS把node节点push到_cxq列表中,同一时刻可能有多个线程把自己的node节点push到_cxq列表中;
3、node节点push到_cxq列表之后,通过自旋尝试获取锁,如果还是没有获取到锁,则通过park将当前线程挂起,等待被唤醒,实现如下:

  1. for (;;) {
  2. if (TryLock (Self) > 0) break ;
  3. assert (_owner != Self, "invariant") ;
  4. if ((SyncFlags & 2) && _Responsible == NULL) {
  5. Atomic::cmpxchg_ptr (Self, &_Responsible, NULL) ;
  6. }
  7. // park self
  8. if (_Responsible == Self || (SyncFlags & 1)) {
  9. TEVENT (Inflated enter - park TIMED) ;
  10. Self->_ParkEvent->park ((jlong) RecheckInterval) ;
  11. // Increase the RecheckInterval, but clamp the value.
  12. RecheckInterval *= 8 ;
  13. if (RecheckInterval > 1000) RecheckInterval = 1000 ;
  14. } else {
  15. TEVENT (Inflated enter - park UNTIMED) ;
  16. Self->_ParkEvent->park() ;//当前线程挂起
  17. }
  18. if (TryLock(Self) > 0) break ;
  19. // The lock is still contested.
  20. // Keep a tally of the # of futile wakeups.
  21. // Note that the counter is not protected by a lock or updated by atomics.
  22. // That is by design - we trade "lossy" counters which are exposed to
  23. // races during updates for a lower probe effect.
  24. TEVENT (Inflated enter - Futile wakeup) ;
  25. if (ObjectMonitor::_sync_FutileWakeups != NULL) {
  26. ObjectMonitor::_sync_FutileWakeups->inc() ;
  27. }
  28. ++ nWakeups ;
  29. // Assuming this is not a spurious wakeup we'll normally find _succ == Self.
  30. // We can defer clearing _succ until after the spin completes
  31. // TrySpin() must tolerate being called with _succ == Self.
  32. // Try yet another round of adaptive spinning.
  33. if ((Knob_SpinAfterFutile & 1) && TrySpin (Self) > 0) break ;
  34. // We can find that we were unpark()ed and redesignated _succ while
  35. // we were spinning. That's harmless. If we iterate and call park(),
  36. // park() will consume the event and return immediately and we'll
  37. // just spin again. This pattern can repeat, leaving _succ to simply
  38. // spin on a CPU. Enable Knob_ResetEvent to clear pending unparks().
  39. // Alternately, we can sample fired() here, and if set, forgo spinning
  40. // in the next iteration.
  41. if ((Knob_ResetEvent & 1) && Self->_ParkEvent->fired()) {
  42. Self->_ParkEvent->reset() ;
  43. OrderAccess::fence() ;
  44. }
  45. if (_succ == Self) _succ = NULL ;
  46. // Invariant: after clearing _succ a thread *must* retry _owner before parking.
  47. OrderAccess::fence() ;
  48. }


4、当该线程被唤醒时,会从挂起的点继续执行,通过ObjectMonitor::TryLock尝试获取锁,TryLock方法实现如下:

  1. int ObjectMonitor::TryLock (Thread * Self) {
  2. for (;;) {
  3. void * own = _owner ;
  4. if (own != NULL) return 0 ;
  5. if (Atomic::cmpxchg_ptr (Self, &_owner, NULL) == NULL) {//CAS成功,获取锁
  6. // Either guarantee _recursions == 0 or set _recursions = 0.
  7. assert (_recursions == 0, "invariant") ;
  8. assert (_owner == Self, "invariant") ;
  9. // CONSIDER: set or assert that OwnerIsThread == 1
  10. return 1 ;
  11. }
  12. // The lock had been free momentarily, but we lost the race to the lock.
  13. // Interference -- the CAS failed.
  14. // We can either return -1 or retry.
  15. // Retry doesn't make as much sense because the lock was just acquired.
  16. if (true) return -1 ;
  17. }
  18. }

其本质就是通过CAS设置monitor的_owner字段为当前线程,如果CAS成功,则表示该线程获取了锁,跳出自旋操作,执行同步代码,否则继续被挂起;

2.3.3.4 monitor释放

当某个持有锁的线程执行完同步代码块时,会进行锁的释放,给其它线程机会执行同步代码,在HotSpot中,通过退出monitor的方式实现锁的释放,并通知被阻塞的线程,具体实现位于ObjectMonitor::exit方法中。

  1. void ATTR ObjectMonitor::exit(TRAPS) {
  2. Thread * Self = THREAD ;
  3. //如果当前线程不是Monitor的所有者
  4. if (THREAD != _owner) {
  5. if (THREAD->is_lock_owned((address) _owner)) { //
  6. // Transmute _owner from a BasicLock pointer to a Thread address.
  7. // We don't need to hold _mutex for this transition.
  8. // Non-null to Non-null is safe as long as all readers can
  9. // tolerate either flavor.
  10. assert (_recursions == 0, "invariant") ;
  11. _owner = THREAD ;
  12. _recursions = 0 ;
  13. OwnerIsThread = 1 ;
  14. } else {
  15. // NOTE: we need to handle unbalanced monitor enter/exit
  16. // in native code by throwing an exception.
  17. // TODO: Throw an IllegalMonitorStateException ?
  18. TEVENT (Exit - Throw IMSX) ;
  19. assert(false, "Non-balanced monitor enter/exit!");
  20. if (false) {
  21. THROW(vmSymbols::java_lang_IllegalMonitorStateException());
  22. }
  23. return;
  24. }
  25. }
  26. // 如果_recursions次数不为0.自减
  27. if (_recursions != 0) {
  28. _recursions--; // this is simple recursive enter
  29. TEVENT (Inflated exit - recursive) ;
  30. return ;
  31. }
  32. //省略部分代码,根据不同的策略(由QMode指定),从cxq或EntryList中获取头节点,通过ObjectMonitor::ExitEpilog方法唤醒该节点封装的线程,唤醒操作最终由unpark完成。

1、如果是重量级锁的释放,monitor中的_owner指向当前线程,即THREAD == _owner;
2、根据不同的策略(由QMode指定),从cxq或EntryList中获取头节点,通过ObjectMonitor::ExitEpilog方法唤醒该节点封装的线程,唤醒操作最终由unpark完成,实现如下: