public class Windows1 implements Runnable {
private int ticket=100;
Object object=new Object();
@Override
public void run() {
while (true){
synchronized (object){
if (ticket>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"出售"+ticket+"号票");
ticket--;
}
else
break;
}
}
}
}
class WindowsTest1{
public static void main(String[] args) {
Windows1 windows1=new Windows1();
Thread thread1=new Thread(windows1);
Thread thread2=new Thread(windows1);
Thread thread3=new Thread(windows1);
thread1.setName("窗口1");
thread2.setName("窗口2");
thread3.setName("窗口3");
thread1.start();
thread2.start();
thread3.start();
}
}