//wait是释放锁,notify是唤醒其他调(用wait等待)的一个线程,不包括自己,notifyAll是唤醒所有线程包括自己
public class Notify implements Runnable {
private String name;
private static Object s=new Object();
private Notify(String name) {
this.name = name;
}
@Override
public void run() {
synchronized (s)
{
for(int i=0;i<10;i++)
{
s.notify();//选取启动的线程不包括本线程,即使本线程用wait终止,也就是说,要有别的线程来激活本线程
//激活最近的一个线程
System.out.println(name+":"+i);
try {
Thread.sleep(1000);
s.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws Exception {
Notify pa = new Notify("A");
Notify pb = new Notify ("B");
Notify pc = new Notify("C");
Notify pd = new Notify("D");
new Thread(pa).start();
new Thread(pb).start();
new Thread(pc).start();
// new Thread(pd).start();
}
}