gpt4 book ai didi

java - Java 对象锁定和监视器创建如何在 JVM 内部发生

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:51:13 24 4
gpt4 key购买 nike

假设我有以下代码片段,其中两个线程访问具有两个关键部分(同步语句)的相同方法。这些同步语句中的每一个都被赋予一个不同的锁对象。代码如下:

public class MyWorker {
private Random random = new Random();

private Object lock1 = new Object();
private Object lock2 = new Object();

private List<Integer> list1 = new ArrayList<>();
private List<Integer> list2 = new ArrayList<>();

private void stageOne() {

synchronized (lock1) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}

list1.add(random.nextInt(100));
}

}

private void stageTwo() {

synchronized (lock2) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}

list2.add(random.nextInt(100));
}

}

private void process() {
for (int i=0; i<1000; i++) {
stageOne();
stageTwo();
}

}

void main() {

Thread t1 = new Thread(this::process);

Thread t2 = new Thread(this::process);

t1.start();
t2.start();

try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

}

}

我的问题不是关于这段代码中的错误,也不是关于它在 java 立场上是如何执行的。此代码工作正常。我只是将此作为引用代码,以便回答的人有一个特定的场景可以引用 我想知道 JVM 如何在内部创建与该实例关联的监视器对象以及如何根据该场景在内部发生对象锁定使用 OpenJDK 实现。 我期待一个低水平的解释。

我对这个主题研究了几天,但找不到深入的解释。这些是我经历过的一些发现:

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. 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; each unlock reverses the effect of one lock operation.

In the Java virtual machine, every object and class is logically associated with a monitor. For objects, the associated monitor protects the object's instance variables. For classes, the monitor protects the class's class variables. If an object has no instance variables, or a class has no class variables, the associated monitor protects no data.

To implement the mutual exclusion capability of monitors, the Java virtual machine associates a lock (sometimes called a mutex) with each object and class. A lock is like a privilege that only one thread can "own" at any one time. Threads need not obtain a lock to access instance or class variables. If a thread does obtain a lock, however, no other thread can obtain a lock on the same data until the thread that owns the lock releases it. (To "lock an object" is to acquire the monitor associated with that object.)

我知道在指令集级别如何使用 monitorentermonitorexit 操作码来管理同步语句。但我正试图通过 JVM 源代码级别获得更深入的了解。但是我正在努力将 OpenJDK 源代码与我通过上面的链接找到的高级解释进行映射,因为源代码中有很多内容。

那么熟悉OpenJDK源代码的人可以使用OpenJDK源代码对以下与上述代码片段相关的问题进行解释吗?我想ObjectMonitor , BasicLock , Synchronizer类与此解释更相关。

  1. 为哪个对象实例创建了一个监控对象?它是用于 MyWorker 对象实例还是用于 Object lock1 或两者?因为 JSL 和 Bill Vennams 的解释描述了每个对象都与一个监视器相关联。
  2. 如果是针对 MyWorker 对象实例,如何为 MyWorker 对象实例创建监视器?
  3. 如何为引用对象创建锁对象 Object lock1我们通过
  4. 线程的锁对象实际上是如何锁定监视器的?

最佳答案

For which object instance a monitor object is created?

每个 Java 对象也是一个监控对象,包括反射对象,因此您的代码至少具有以下内容:

  • MyWorker 的类对象
  • Random 的类对象
  • Object 的类对象
  • List 的类对象
  • Integer 的类对象
  • ArrayList 的类对象
  • ...还有更多...
  • Random 实例分配给字段 random
  • Object 实例分配给字段 lock1
  • Object 实例分配给字段 lock2
  • ArrayList 实例分配给字段 list1
  • ArrayList 实例分配给字段 list2
  • Thread 实例分配给局部变量 t1
  • Thread 实例分配给局部变量 t2
  • 调用 add(random.nextInt(100)) 时自动装箱创建的每个 Integer 实例

Is it for MyWorker object instance or Object lock1 or both?

两者

If it's for MyWorker object instance how monitor is created for the MyWorker object instance?
How lock object is created for the reference object Object lock1 we pass
How actually monitor is locked by the lock object for a Thread?

取决于 JVM 内部结构。对此没有单一的答案,“彻底的解释”超出了本网站的范围。

关于java - Java 对象锁定和监视器创建如何在 JVM 内部发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50956803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com