package com.pb.thread;
/*
* 模拟一百张票,四个窗口售票的实现
* */
public class TicketSeller extends Thread{
private static int tickets=100; //这一百张票是四个窗口共享的,因此应该用static修饰
// private static TicketSeller ts=new TicketSeller(); //创建一个共享的TicketSeller对象
public void run(){
synchronized(TicketSeller.class){
while(true){
if(tickets<=0){
break;
}
System.out.println(getName()+"票号:"+tickets);
tickets--;
}
}
}
}
package com.pb.thread;
public class SellTicket {
public static void main(String[] args) {
new TicketSeller().start();
new TicketSeller().start();
new TicketSeller().start();
new TicketSeller().start();
}
}
break用在循环语句和switch语句中,continue用在循环语句中&#x