这是一个代码片段
public class ITC3 extends Thread {
private ITC4 it;
public ITC3(ITC4 it){
this.it = it;
}
public static void main(String[] args) {
ITC4 itr = new ITC4();
System.out.println("name is:" + itr.getName());
ITC3 i = new ITC3(itr);
ITC3 ii = new ITC3(itr);
i.start(); ii.start();
//iii.start();
try{
Thread.sleep(1000);
}catch(InterruptedException ie){}
itr.start();
}
public void run(){
synchronized (it){
try{
System.out.println("Waiting - " + Thread.currentThread().getName());
it.wait();
System.out.println("Notified " + Thread.currentThread().getName());
}catch (InterruptedException ie){}
}
}
}
class ITC4 extends Thread{
public void run(){
try{
System.out.println("Sleeping : " + this);
Thread.sleep(3000);
synchronized (this){
this.notify();
}
}catch(InterruptedException ie){}
}
}
给出的输出是
Output:
Waiting - Thread-1
Waiting - Thread-2
Sleeping : Thread[Thread-0,5,main]
Notified Thread-1
Notified Thread-2
在此所有线程都得到通知。在这种情况下,我无法理解整个输出。
- 为什么所有线程都会收到通知?
- 为什么 Sleeping 正在打印 `Thread[Thread-0,5,main]
- 完全迷失在程序的整个工作中。
任何指针都会有所帮助。
谢谢。