package cn.itcast.thread;
/*
* 多线程技术,模拟实际售票效果
* 总数100张,3个窗口同时进行售票,3个线程
*
*
*
*/
class ThreadTicket extends Thread{
private static int tickets = 100;
Object obj=new Object;
public void run(){
while(true){
synchronized(obj){
if(tickets > 0){
System.out.println(getName()+ " 出售第"+tickets--);
}
}
}
}
}
public class ThreadDemo7 {
public static void main(String[] args) {
ThreadTicket tt0 = new ThreadTicket();
ThreadTicket tt1 = new ThreadTicket();
ThreadTicket tt2 = new ThreadTicket();
tt0.start();
tt1.start();
tt2.start();
}
}
|
|