package com.itheima;
class SellTicket implements Runnable
{
private int ticket = 100;
@Override
public void run()
{
//在SellTicket类中加上同步代码块即可
// synchronized (SellTicket.class)
// {
while(ticket > 0)
{
System.out.println(Thread.currentThread().getName() + "卖出第" + ticket +"张票");
ticket--;
}
// }
}
}
public class Test025
{
/**
* @param args
*/
public static void main(String[] args)
{
//创建一个SellTicket对象
SellTicket st = new SellTicket();
//创建四个售票窗口线程,将st对象传给Thread类的构造函数
Thread ticketwindow1 = new Thread(st);
Thread ticketwindow2 = new Thread(st);
Thread ticketwindow3 = new Thread(st);
Thread ticketwindow4 = new Thread(st);
//启动四个窗口
ticketwindow1.start();
ticketwindow2.start();
ticketwindow3.start();
ticketwindow4.start();
}
}
|
|