class DeadLockDemo
{
public static void main(String[] args)
{
Ticket ticket = new Ticket();
for( int i = 1;i<10;i++)
{
new Thread(ticket,"thread--00"+ i +"--").start();
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
ticket.flag = !ticket.flag;
}
}
}
class Ticket implements Runnable
{
private int num = 10000;
Object object = new Object();
boolean flag = true;
public void run()
{
if(flag)
{
while( true)
{
synchronized(object)
{
sale();
}
}
}
else
{
while( true)
{
sale();
}
}
}
public synchronized void sale()
{
synchronized(object)
{
if(num>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
}
System.out.println(Thread.currentThread().getName()+" .. sale .. "+num--);
}
}
}
}
{
public static void main(String[] args)
{
Ticket ticket = new Ticket();
for( int i = 1;i<10;i++)
{
new Thread(ticket,"thread--00"+ i +"--").start();
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
ticket.flag = !ticket.flag;
}
}
}
class Ticket implements Runnable
{
private int num = 10000;
Object object = new Object();
boolean flag = true;
public void run()
{
if(flag)
{
while( true)
{
synchronized(object)
{
sale();
}
}
}
else
{
while( true)
{
sale();
}
}
}
public synchronized void sale()
{
synchronized(object)
{
if(num>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
}
System.out.println(Thread.currentThread().getName()+" .. sale .. "+num--);
}
}
}
}