public class Test96 {
public static void main(String[] args) {
Window96 w=new Window96();
Thread t1=new Thread(w);
Thread t2=new Thread(w);
t1.setName("窗口1");
t2.setName("窗口2");
t1.start();
t2.start();
}
}
//线程的通信
public class Window96 implements Runnable{
private int ticket=100;
@Override
public void run() {
while (true){
synchronized (this){
notify();
if (ticket>0){
System.out.println(Thread.currentThread().getName()+":"+ticket);
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
ticket--;
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else {
break;
}
}
}
}
}