Java同步似乎被忽略了
我有以下代码,我预计在打印出“Main:预同步”后会死锁。但看起来 synchronized
并没有达到我的预期。这里会发生什么?
import java.util.*;
public class deadtest {
public static class waiter implements Runnable {
Object obj;
public waiter(Object obj) {
this.obj = obj;
}
public void run() {
System.err.println("Thead: pre-sync");
synchronized(obj) {
System.err.println("Thead: pre-wait");
try {
obj.wait();
} catch (Exception e) {
}
System.err.println("Thead: post-wait");
}
System.err.println("Thead: post-sync");
}
}
public static void main(String args[]) {
Object obj = new Object();
System.err.println("Main: pre-spawn");
Thread waiterThread = new Thread(new waiter(obj));
waiterThread.start();
try {
Thread.sleep(1000);
} catch (Exception e) {
}
System.err.println("Main: pre-sync");
synchronized(obj) {
System.err.println("Main: pre-notify");
obj.notify();
System.err.println("Main: post-notify");
}
System.err.println("Main: post-sync");
try {
waiterThread.join();
} catch (Exception e) {
}
}
}
由于两个线程在创建的对象上同步,因此我预计线程实际上会相互阻塞。目前,代码愉快地通知其他线程、加入和退出。
I've got the following code, which I expected to deadlock after printing out "Main: pre-sync". But it looks like synchronized
doesn't do what I expect it to. What happens here?
import java.util.*;
public class deadtest {
public static class waiter implements Runnable {
Object obj;
public waiter(Object obj) {
this.obj = obj;
}
public void run() {
System.err.println("Thead: pre-sync");
synchronized(obj) {
System.err.println("Thead: pre-wait");
try {
obj.wait();
} catch (Exception e) {
}
System.err.println("Thead: post-wait");
}
System.err.println("Thead: post-sync");
}
}
public static void main(String args[]) {
Object obj = new Object();
System.err.println("Main: pre-spawn");
Thread waiterThread = new Thread(new waiter(obj));
waiterThread.start();
try {
Thread.sleep(1000);
} catch (Exception e) {
}
System.err.println("Main: pre-sync");
synchronized(obj) {
System.err.println("Main: pre-notify");
obj.notify();
System.err.println("Main: post-notify");
}
System.err.println("Main: post-sync");
try {
waiterThread.join();
} catch (Exception e) {
}
}
}
Since both threads synchronize on the created object, I expected the threads to actually block each other. Currently, the code happily notifies the other thread, joins and exits.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在监视器上调用
.wait()
实际上会释放同步锁,以便其他线程可以锁定同一监视器并发送通知。您的行为完全正常:“服务员”锁定监视器,然后在等待通知时释放锁定。 1秒后,主线程锁定监视器,发送通知,解锁监视器,从而唤醒等待者以完成其操作。
Calling
.wait()
on a monitor actually releases the synchronized lock so the other thread can lock on to the same monitor and send a notification.Your behavior is completly normal: "waiter" locks on a monitor and then releases the lock when waiting for notification. After 1 second the main thread locks the monitor, sends notification, unlocks the monitor, which wakes the waiter to complete its operation.
当您对某个对象
wait()
时,该线程会释放该对象上的锁,以允许其他线程获取该锁并notify()
等待线程。请参阅 javadoc 了解Object.wait()
。When you
wait()
on an object, the thread releases the lock on the object to allow others to aquire the lock andnotify()
the waiting thread. See the javadoc forObject.wait()
.