//先定义一个Ticket类
class Ticket
{
private String hcp;//数量
public Ticket(String paramHcp)
{
this.hcp = paramHcp;
}
public String getHcp(){
return this.hcp;
}
}
//在SealWindow中打印Ticket的相应值(数量)
public class SealWindow implements Runnable
{
private static in tick = 100;
boolean flag = true;
public void run()
{
if(flag)
{
while(true)
{
synchronized(SealWindow.class)
{
if(tick>0)
{
try(Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getBane()+tick--);
}
}
}
}
else
while(true)
show();
}
public static synchronized void show()
{
if(tick>0)
{
try(Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getBane()+tick--);
}
}
}
class TicketSealCenter
{
public static void main(String[] args)
{
Ticket tk = new Ticket(100);
System.out.println("票数: "+tk.getSl());
Ticket t = new Ticket();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
t1.start();
try{Thread.sleep(10);}catch(Exception e){}
t.flag = false;
t2.start();
}
} |
|