32

Java并发—Synchronized 的本质

 3 years ago
source link: http://www.demanmath.com/index.php/2020/07/24/javabingfa-synchronized-debenzhi/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

使用场景

先用一张图来描述使用的场景(图片获取至其他网站。)

NzQ36zz.png!web

总体来说就是方法和代码块2种方式。

代码块的同步

每个object在设计的时候,都是可以被monitor的,被锁住的,所以使用计数器的方式就可以解决这个锁的问题。

方法同步

这个按上一篇说讲,就是方法通过ACC_SYNCHRONIZED标志位来控制锁。

xxx.class锁住的是什么?

只有static的方法可以synchronized xxx.class,那这个xxx.class是什么。

我们先理解static method是什么。按照深入理解JVM的说法,static的本质是一套命名方式,也就是与实例xxx class没有任何本质的关系。

那这些static的方法是存在方法区的固定code,不需要实例化。

我们在回来看这个问题,当实例对象的方法被synchronized的时候,它同步锁住的是什么?就是this。虽然在JVM实现的时候不同,但是在更底层的时候是一样的。

在会过来,既然锁最终是总过对象来持有monitor的,那static的方法,是谁持有?synchroinzed的是什么?

我们继续反编译代码来分析

public static void testB();
    Code:
       0: ldc           #2                  // class com/demanmath/androidms/javabase/concurrent/SynchronizedDemo
       2: dup
       3: astore_0
       4: monitorenter
       5: iconst_0
       6: istore_1
       7: aload_0
       8: monitorexit
       9: goto          17
      12: astore_2
      13: aload_0
      14: monitorexit
      15: aload_2
      16: athrow
      17: return
    Exception table:
       from    to  target type
           5     9    12   any
          12    15    12   any

基本和非static的方法是一直的。也就是也有一个东西,被monitor了

这个东西就是可以被索引这个static方法的Class。所以Synchronized的就是这个Class。对,不是实例,是这个Class。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK