根据 Oracle 文档 Intrinsic Locks and Synchronization :
Intrinsic locks play a role in both aspects of synchronization: enforcing exclusive access to an object's state and establishing happens-before relationships that are essential to visibility.
我的问题是:
这里的发生在关系之前在同步上下文中意味着什么?这是否意味着一旦一个线程执行同步块(synchronized block),所有更改都会在其他线程到来并重新获取锁之前刷新到主内存?
我还有一个问题:
假设线程1已获取锁并正在执行同步块(synchronized block)。
JVM/线程调度程序是否可以强制线程1进入另一个状态(例如等待),以便释放锁并释放另一个状态线程说 thread2 将获取锁?现在,一段时间后,thread1 可以再次进入运行状态并从失去锁的位置开始。
最佳答案
what does happens-before relationships here means in synchronization context?
这种关系简单来说就是guarantee一个特定语句写入的内存对另一特定语句可见。在这种情况下,这意味着当您在同步块(synchronized block)中写入一些内容时,进入同步块(synchronized block)的其他线程将会注意到这种变化。
Is it possible for JVM/thread scheduler to force the thread1 to another state say wait such that it will release the lock and another thread say thread2 will acquire the lock?
不可以,但线程可以通过调用wait
来释放锁。
关于java - Java 中的内在锁和同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54072388/