车站卖票,
private int ticket = 10;
//卖票操作
public void run() {
for(int i=0;i<100;i++){
if(this.ticket>0){
ticket--;
System.out.println((Thread.currentThread().getName())+"窗口,卖出的第:"+ticket+"张票");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public class TicketDemo {
public static void main(String[] args) {
Ticket02 t1 = new Ticket02();//共享资源
Thread th1 = new Thread(t1);//开启线程,给线程重新命名
th1.start();
new Thread(t1,"线程A").start();//把每个售票窗口看作一个线程
new Thread(t1,"线程B").start();
new Thread(t1,"线程C").start();
new Thread(t1,"线程D").start();
}
}