【发布时间】:2017-10-28 11:50:30
【问题描述】:
您好,我是 java 线程的新手。我想弄清楚什么是 wait() 和 notify() 方法。所以我写了简单的程序,但我不能 找到无限执行的原因。请问有人可以帮我解决这个问题吗?
public class StairCase {
public static void main(String[] args) {
int[] lst = {1,2,3,4,5,6,7};
Test1 t1 = new Test1(lst);
t1.setName("Test 1 ");
Test2 t2 = new Test2(lst);
t2.setName("Test 2 ");
t1.start();
t2.start();
}
}
class Test1 extends Thread {
int[] line ;
public Test1(int[] lst) {
this.line = lst;
}
public void run(){
synchronized(line) {
for (int i = 0; i < 5; i++) {
try{
if(i == 2) line.wait();
} catch (Exception e) {
}
System.out.println(Thread.currentThread().getName() + line[i]);
}
}
}
}
class Test2 extends Thread {
int[] line ;
public Test2(int[] lst) {
this.line = lst;
}
public void run() {
synchronized(line) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + line[i]);
}
}
try {
line.notify();
} catch(Exception e) {
}
}
}
【问题讨论】:
-
你的任务完成后有没有尝试过system.exit(0)?
-
你的代码在第 45 行得到了 java.lang.IllegalMonitorStateException。
-
是的。我认为问题出在线程中。但我无法弄清楚。
-
catch (Exception ex) { }是在尝试调试程序时彻底迷惑自己的绝佳方式。当异常发生时,它们会悄无声息地消失在一个深深的黑洞中,而你却一无所知。绝不,绝不,绝不,绝不在任何情况下对自己造成如此残酷的伤害。至少把ex.printStackTrace();放在那里。 -
o.wait() 和 o.notify() 的 javadoc 没有解释它们的用途。请参阅教程:docs.oracle.com/javase/tutorial/essential/concurrency/…
标签: java multithreading concurrency wait notify