日期:2014-05-20  浏览次数:20652 次

一道关于线程的题目,求解!!!!!
为Thread撰写两个子类,其中一个的run()在启动后取得第二个Thread   object   reference,然后调用wait()。另一个子类的run()在过了数秒之后调用notifyAll(),唤醒第一个线程,使第一个线程可以印出消息。

------解决方案--------------------
public class Thread1 extends Thread{
public void run(){
Thread1 t2 = new Thread1();
t2.start();
try{
synronized(t2){
t2.wait();
}
}catch(Exception e){ // 忘了异常具体类型了,好象是ThreadInterruptException
System.out.println( "Thread 2 notified after Thread 1 pause 5 seconds. ");
}
}
}
public class Thread2 extends Thread{
public void run(){
try{
Thread.sleep(5000);
synchronized(this){
this.notifyAll();
}
}catch(Exception e){// 同上

}
}
}
忘了哪个包里的了,所有都没有import,有笔误请原谅