/**
* 7、 编写三各类Ticket、SealWindow、TicketSealCenter分别代表票信息、售票窗口、售票中心。
* 售票中心分配一定数量的票,由若干个售票窗口进行出售,利用你所学的线程知识来模拟此售票过程。
*
*/
class test
{
public static void main(String[] args)
{
windows.openwindows();
}
}
class tickets implements Runnable
{
String name;
tickets(String name)
{
this.name=name;
}
static int tickets = 100;
public void run()
{
saleticket();
}
public synchronized void saleticket()
{
while(tickets>0)
{
System.out.println(this.name+"..."+tickets);
System.out.println(this.name+"卖出了第"+(tickets--)+"张票");
}
}
}
class windows
{
public static void openwindows()
{
for(int x=1;x<=5;x++)
{
tickets tic = new tickets("售票窗口"+x);
Thread t = new Thread(tic);
t.start();
}
}
}
上面这段代码 saleticket()函数已经设置为同步,为什么还会有问题? |
|