package Tickets;
public class Tickets implements Runnable
{
int num = 1;
@Override
public void run()
{ while(true) {
synchronized (this)
{ try
{
Thread.sleep(300);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if(num<=100) {
System.out.println(Thread.currentThread().getName()+"正在卖第"+num++ +"张票");
}
else {
System.out.println("票已经卖完了!");
break;
}
}
}
}
}
package Tickets;
public class TicketsDemo
{
/**
* @param args
*/
public static void main(String[] args)
{
//创建线程目标类对象
Tickets ticket = new Tickets();
//创建线程对象
Thread t1 = new Thread(ticket,"窗口1");
Thread t2 = new Thread(ticket,"窗口2");
Thread t3 = new Thread(ticket,"窗口3");
Thread t4 = new Thread(ticket,"窗口4");
//开启线程
t1.start();
t2.start();
t3.start();
t4.start();
}
|
|