英文:
Questions about the concurrency‘s synchronized, how it works, what is the scope
问题
我正在尝试理解 Java 中的 synchronized 如何工作,我已经阅读了一些文档,但能否有人帮忙检查一下正确性,还有一些问题。
对于代码块:
public class MyWaitNotify3 {
MonitorObject myMonitorObject = new MonitorObject();
boolean wasSignalled = false;
public void doWait() {
synchronized (myMonitorObject) {
while (!wasSignalled) {
try {
myMonitorObject.wait();
} catch (InterruptedException e) {...}
}
//clear signal and continue running.
wasSignalled = false;
}
}
public void doNotify() {
synchronized (myMonitorObject) {
wasSignalled = true;
myMonitorObject.notify();
}
}
}
MyWaitNotify3 inst1 = new MyWaitNotify3();
MyWaitNotify3 inst2 = new MyWaitNotify3();
如果我们使用 inst1 创建一个线程,使用 inst2 创建另一个线程,然后在线程1中调用 thread1.doWait(),那么该线程将在行 "myMonitorObject.wait();" 处挂起。问题是,在 "dowait"、"synchronized(myMonitorObject)" 的范围内的锁会阻止线程2 在 "doNotify()" 中获取锁吗?
根据我的理解,由于它们是两个实例,线程2 不能在 doWait 中获取 "synchronized(myMonitorObject)",但可以在 doNotify 中获取。这个理解正确吗?
如果我们定义如下:
MyWaitNotify3 inst1 = new MyWaitNotify3();
Thread thread1 = new Thread(inst1, "A");
Thread thread2 = new Thread(inst1, "B");
英文:
I am trying to understand how java's synchronized works, and I have read some document, but would somebody helps check the correctness and also the questions
For the code block
public class MyWaitNotify3{
MonitorObject myMonitorObject = new MonitorObject();
boolean wasSignalled = false;
public void doWait(){
synchronized(myMonitorObject){
while(!wasSignalled){
try{
myMonitorObject.wait();
} catch(InterruptedException e){...}
}
//clear signal and continue running.
wasSignalled = false;
}
}
public void doNotify(){
synchronized(myMonitorObject){
wasSignalled = true;
myMonitorObject.notify();
}
}
}
MyWaitNotify3 inst1 = new MyWaitNotify3();
MyWaitNotify3 inst2 = new MyWaitNotify3();
If we create one thread with inst1, another thread with inst2
Then if we do thread1.doWait(), then the thread will hangs at the line "myMonitorObject.wait();"
The question is that the lock in scope of "dowait", "synchronized(myMonitorObject)", will this blocks thread2 from acquire the lock in "doNotify()"??
From my understanding, since they are two instances, thread2 cannot acquire the "synchronized(myMonitorObject)" in doWait, but would acquire the "synchronized(myMonitorObject)" in doNotify. Is that correct?
What will happens if we define
MyWaitNotify3 inst1 = new MyWaitNotify3();
thread1 = new Thread(inst1, "A");
thread2 = new Thread(inst1, "B");
答案1
得分: 0
> 问题是,在“dowait”、“synchronized(myMonitorObject)”范围内,这会阻止线程2在“doNotify()”中获取锁吗?
不会。当线程在“doWait”中使用“wait”进入等待状态时,它会释放临界区(使用synchronized
块)的锁。在此期间,另一个线程可以在“doNotify”中获取该锁,进入临界区。当等待线程离开等待状态时,它必须重新获取锁。
然而,当处于“doWait”中的线程正在运行,即未在“wait”中阻塞时,它当然会持有当前临界区(即myMonitorObject
)的锁,这将阻止“doNotify”中的另一个线程。
你可以想象“wait”是一个使线程离开临界区、释放锁的点,从而使其他线程能够获取该锁的点,也可以将其视为进入临界区的入口点,在此处线程必须重新获取锁,这涉及阻塞。
英文:
> The question is that the lock in scope of "dowait", "synchronized(myMonitorObject)", will this blocks thread2 from acquire the lock in "doNotify()"??
No, it won't. A thread will release the lock for the critical section (synchronized
-block) when it goes into waiting state with wait
in doWait
. In that time, another thread can acquire that lock for the critical section in doNotify
. When the waiting thread leaves the waiting state, it has to acquire the lock again.
However, when the thread in doWait
is running, i.e. not blocked in wait
, it will of course hold the current lock for the section, i.e. myMonitorObject
, which will block another thread in doNotify
.
You can imagine wait
to be a point where a thread leaves the critical section, releases the lock, which enables others to acquire it, and as an entrance point for the critical section where the thread has to re-acquire the lock for it, which involves blocking.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论