帮我看一下吧没,大神们
package ThreadTest1;
///*
//* 编写三各类Ticket、SealWindow、TicketSealCenter
//* 分别代表票信息、售票窗口、售票中心。
//* 售票中心分配一定数量的票,由若干个售票窗口进行出售,
//* 利用你所学的线程知识来模拟此售票过程。
//*
//* 票信息:定义总票数,由售票中心设置值;
//* 售票窗口:窗口N售出第m张票
//* 售票中心:创建线程,给Ticket分配票,开启线程
//*/
//
public class Test5 {
public static void main(String[] args) {
Ticket t=Ticket.getTicket();
t.setTicket(300);//分配票数;
SellTicket st=new SellTicket(t);
Thread t1 = new Thread(st, "窗口1");
Thread t2 = new Thread(st, "窗口2");
Thread t3 = new Thread(st, "窗口3");
Thread t4 = new Thread(st, "窗口4");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Ticket{
//懒汉式单例
private Ticket(){
}
private static Ticket t=null;
public synchronized static Ticket getTicket(){
if(t==null){
t=new Ticket();
}
return t;
}
//定义票数
private int totalTicket=0;
//中心分配票功能
public void setTicket(int totalTicket){
this.totalTicket=totalTicket;
}
//窗口得到票数功能
public int getTotalTicket(){
return totalTicket;
}
}
class SellTicket implements Runnable{
private Ticket t;
public SellTicket(Ticket t){
this.t=t;
}
int ticket=t.getTotalTicket();
public void run(){
while(ticket>=1){
synchronized(this){
if(ticket>=1){
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "售出第"
+ (ticket--) + "张票");
}
}
}
}
}
|