【发布时间】:2013-01-06 21:16:08
【问题描述】:
为什么线程不等待notify()?线程启动,然后进入等待池,但在那一刻之后继续执行。
public class JavaApplication2 {
public static void main(String [] args) {
ThreadB b = new ThreadB();
synchronized(b) {
b.start();
try {
System.out.println("1");
b.wait();
} catch (InterruptedException e) {}
System.out.println("Total is: " + b.total);
}
}
}
class ThreadB extends Thread {
int total;
@Override
public void run() {
synchronized(this) {
total += 1;
//notify();
}
}
}
【问题讨论】:
-
什么通知?我没有看到任何通知,除了注释掉的那个。
-
没错,线程必须等待永恒,但事实并非如此。
-
试试
b.join()而不是wait
标签: java concurrency wait notify