多个线程不是可以同时进入一个synchronized块吗?
Are not multiple threads able to enter a synchronized block at the same time?
我是 Java 的新手,在了解 java 中的多线程时遇到了这个链接:http://tutorials.jenkov.com/java-concurrency/slipped-conditions.html。
在本教程中调用以下代码是避免出现错误情况的良好做法:
public class Lock {
private boolean isLocked = true;
public void lock(){
synchronized(this){
while(isLocked){
try{
this.wait();
} catch(InterruptedException e){
//do nothing, keep waiting
}
}
isLocked = true;
}
}
public synchronized void unlock(){
isLocked = false;
this.notify();
}
}
我怀疑如果两个线程 A 和 B 同时调用 lock() 并且 isLocked 为真,即锁已被其他线程 C 占用。现在:
--1 A先进入synchronized块(因为只有一个人可以获得monitor-object this的锁并进入synchronized块)
--2 A 调用 this.wait() 并因此释放对监视器对象的锁定(wait() 调用释放对监视器对象 http://tutorials.jenkov.com/java-concurrency/thread-signaling.html#wait-notify 的锁定)但仍保留在同步块内
--3 现在 B 进入同步块(因为 A 已经释放了对 monitor-object this 的锁定)
--4 B 调用 this.wait() 并因此释放对监视器对象的锁定(wait() 调用释放对监视器对象的锁定)
--5 此时线程 C 调用 unlock() 即设置 isLocked 为 false 并调用 this.notify()
--6 现在A和B其中一个出来wait(),然后从while循环中出来,设置isLocked为true
--7 循环继续
所以--3中,A和B同时在一个synchronized块中,是不是违反了synchronized块中一次只能有一个线程的基本多线程原则?
请澄清我的疑问。
线程只能从 wait() 方法 return 如果它重新获取其正在等待的对象的锁。在您的场景中,A 和 B 将竞争获得锁,只有其中一个获得它,另一个将一直等待直到再次释放锁。
来自 the javadoc(强调我的):
The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
我是 Java 的新手,在了解 java 中的多线程时遇到了这个链接:http://tutorials.jenkov.com/java-concurrency/slipped-conditions.html。
在本教程中调用以下代码是避免出现错误情况的良好做法:
public class Lock {
private boolean isLocked = true;
public void lock(){
synchronized(this){
while(isLocked){
try{
this.wait();
} catch(InterruptedException e){
//do nothing, keep waiting
}
}
isLocked = true;
}
}
public synchronized void unlock(){
isLocked = false;
this.notify();
}
}
我怀疑如果两个线程 A 和 B 同时调用 lock() 并且 isLocked 为真,即锁已被其他线程 C 占用。现在:
--1 A先进入synchronized块(因为只有一个人可以获得monitor-object this的锁并进入synchronized块) --2 A 调用 this.wait() 并因此释放对监视器对象的锁定(wait() 调用释放对监视器对象 http://tutorials.jenkov.com/java-concurrency/thread-signaling.html#wait-notify 的锁定)但仍保留在同步块内 --3 现在 B 进入同步块(因为 A 已经释放了对 monitor-object this 的锁定) --4 B 调用 this.wait() 并因此释放对监视器对象的锁定(wait() 调用释放对监视器对象的锁定) --5 此时线程 C 调用 unlock() 即设置 isLocked 为 false 并调用 this.notify() --6 现在A和B其中一个出来wait(),然后从while循环中出来,设置isLocked为true --7 循环继续
所以--3中,A和B同时在一个synchronized块中,是不是违反了synchronized块中一次只能有一个线程的基本多线程原则?
请澄清我的疑问。
线程只能从 wait() 方法 return 如果它重新获取其正在等待的对象的锁。在您的场景中,A 和 B 将竞争获得锁,只有其中一个获得它,另一个将一直等待直到再次释放锁。
来自 the javadoc(强调我的):
The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.