class TestSYN2 {
public static void main(String[] agrs) throws Exception{
myrunable2 my2=new myrunable2();
Thread th1=new Thread(my2,"A");
Thread th2=new Thread(my2,"B");
Thread th3=new Thread(my2,"C");
th1.start();
th2.start();
th3.start();
}
}
class myrunable2 implements Runnable{
int ticket=10;
int count = 0;
Object obj=new Object();
@Override
public void run() {
while(true){
synchronized(obj){
if(ticket<=0)
return;
else
{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(count % 3 == 0 && Thread.currentThread().getName() == "A"){
System.out.println(Thread.currentThread().getName()+"-"+ticket);
count ++;
}else if(count % 3 == 1 && Thread.currentThread().getName() == "B"){
System.out.println(Thread.currentThread().getName()+"-"+ticket);
count ++;
}else if(count % 3 == 2 && Thread.currentThread().getName() == "C"){
System.out.println(Thread.currentThread().getName()+"-"+ticket--);
count ++;
}
}
obj.notify();
if(ticket<=0)
break;
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}