简体   繁体   English

什么原语用于实现synchronized关键字?

[英]What primitive is used to implement the synchronized keyword?

When we use synchronized keyword in java, which synchronization primitive is used exactly? 当我们在java中使用synchronized关键字时,究竟使用了哪个同步原语? Lock, Semaphore, Monitor, Mutex ? Lock, Semaphore, Monitor, Mutex

EDIT : How JVM implements the lock at the native level ? 编辑: JVM如何在本机级别实现锁定?

At bytecode level, java has monitorenter and monitorexit operations, documented in this page of The Java Virtual Machine Specification , with snippets pasted below ( objectref is operand for the operation, taken from stack): 在字节码级别,java具有monitorentermonitorexit操作,在Java虚拟机规范的此页面中记录,下面粘贴了片段( objectref是操作的操作数,取自堆栈):

monitorenter snippet monitorenter片段

Each object has a monitor associated with it. 每个对象都有一个与之关联的监视器。 The thread that executes monitorenter gains ownership of the monitor associated with objectref . 执行monitorenter的线程获得与objectref关联的监视器的所有权。 If another thread already owns the monitor associated with objectref , the current thread waits until the object is unlocked, then tries again to gain ownership. 如果另一个线程已经拥有与objectref关联的监视器,则当前线程将等待,直到该对象被解锁,然后再次尝试获得所有权。 If the current thread already owns the monitor associated with objectref , it increments a counter in the monitor indicating the number of times this thread has entered the monitor. 如果当前线程已经拥有与objectref关联的监视器,它会增加监视器中的计数器,指示此线程进入监视器的次数。 If the monitor associated with objectref is not owned by any thread, the current thread becomes the owner of the monitor, setting the entry count of this monitor to 1. 如果与objectref关联的监视器不属于任何线程,则当前线程将成为监视器的所有者,并将此监视器的条目数设置为1。

monitorexit snippet monitorexit片段

The current thread should be the owner of the monitor associated with the instance referenced by objectref . 当前线程应该是与objectref引用的实例关联的监视器的所有者。 The thread decrements the counter indicating the number of times it has entered this monitor. 线程递减计数器,指示它进入此监视器的次数。 If as a result the value of the counter becomes zero, the current thread releases the monitor. 如果结果计数器的值变为零,则当前线程释放监视器。 If the monitor associated with objectref becomes free, other threads that are waiting to acquire that monitor are allowed to attempt to do so. 如果与objectref关联的监视器变为空闲,则允许等待获取该监视器的其他线程尝试这样做。

So, "monitor" is the answer, and neither this, nor JLS referenced in NPE's answer specify what happens at native code level. 因此,“监视器”就是答案,在NPE的答案中引用的这个和JLS都没有指明在本机代码级别发生的事情。 If you have a specific platform (CPU and operating system) and a specfic JVM implementation (including version) in mind, you can of course either look at the JVM source (if it is an open source JVM), or ask here. 如果您有一个特定的平台(CPU和操作系统)和一个特定的JVM实现(包括版本),您当然可以查看JVM源(如果它是一个开源JVM),或者在这里询问。

I also happened across this blog from 1997 , which has more details. 我也发生在1997年的这个博客上 ,其中有更多细节。

From the JLS ( §17.1. Synchronization ): 来自JLS( §17.1。同步 ):

The Java programming language provides multiple mechanisms for communicating between threads. Java编程语言为线程之间的通信提供了多种机制。 The most basic of these methods is synchronization, which is implemented using monitors . 这些方法中最基本的是同步, 它是使用监视器实现的 Each object in Java is associated with a monitor, which a thread can lock or unlock. Java中的每个对象都与一个监视器相关联,一个线程可以锁定或解锁。 Only one thread at a time may hold a lock on a monitor. 一次只有一个线程可以锁定监视器。 Any other threads attempting to lock that monitor are blocked until they can obtain a lock on that monitor. 尝试锁定该监视器的任何其他线程都将被阻止,直到它们可以获得该监视器上的锁定为止。 A thread t may lock a particular monitor multiple times; 线程t可以多次锁定特定监视器; each unlock reverses the effect of one lock operation. 每次解锁都会逆转一次锁定操作的效果。

Thus "monitor" is the answer to your first question. 因此,“监视器”是您第一个问题的答案。

As to the second question, this is an unspecified implementation detail. 关于第二个问题,这是一个未指明的实现细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM