import java.util.*;
public class Main
{
public static void main(String[] args)
{
test t =new test();
Thread a=new Thread(t);
a.start();
Thread b=new Thread(t);
b.start();
Thread c=new Thread(t);
c.start();
Thread d=new Thread(t);
d.start();
}
}
class test implements Runnable
{
@Override
public void run()
{
// TODO: Implement this method
Object lock =new Object();//定义一个锁
int tickets =5;
while(tickets>=0)
{
//if(lock==0)//判断锁是否打开。可以不写
//{
//System.out.println("有其他线程正在执行,这次执行取消");
//}
synchronized(lock){//以lock对象为锁,定义同步代码块
tickets--;
System.out.println( Thread.currentThread().getName()+"正在出售第"+tickets+"张票");
}
}
}
}
public class Main
{
public static void main(String[] args)
{
test t =new test();
Thread a=new Thread(t);
a.start();
Thread b=new Thread(t);
b.start();
Thread c=new Thread(t);
c.start();
Thread d=new Thread(t);
d.start();
}
}
class test implements Runnable
{
@Override
public void run()
{
// TODO: Implement this method
Object lock =new Object();//定义一个锁
int tickets =5;
while(tickets>=0)
{
//if(lock==0)//判断锁是否打开。可以不写
//{
//System.out.println("有其他线程正在执行,这次执行取消");
//}
synchronized(lock){//以lock对象为锁,定义同步代码块
tickets--;
System.out.println( Thread.currentThread().getName()+"正在出售第"+tickets+"张票");
}
}
}
}