本帖最后由 邱成 于 2012-9-18 21:31 编辑
学习线程,在做个练习
public class Blocked extends Thread {
public Blocked() {
start();
}
public void run() {
try {
wait();
} catch (InterruptedException e) {
...
}
}
public static void main(String[] args) {
new Blocked();
}
}
跑起来坏掉了
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:474)
at Blocked.run(Blocked.java:8)
把wait()放到同步代码块中就可以了:
synchronized (this) {
wait();
}
这是为什么呢? |
|