package com.lj.thread;
public class SellThread implements Runnable
{
private int number;
String s=new String();
SellThread(){
number=100;
}
@Override
public void run()
{
while(number>0){
synchronized (s)
{
if(number>0){
System.out.println("第"+number+"人在"+Thread.currentThread().getName()+"买票");
number--;
}
}
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
SellThread st=new SellThread();
Thread th1=new Thread(st);
th1.start();
Thread t2=new Thread(st);
t2.start();
Thread t3=new Thread(st);
t3.start();
}
}
Java多线程买票的实例
最新推荐文章于 2024-02-15 21:13:31 发布