import java.util.concurrent.Semaphore;
public class test {
private static Semaphore s1 = new Semaphore(1);
private static Semaphore s2 = new Semaphore(0);
private static Semaphore s3 = new Semaphore(0);
private static int num = 6;
static Thread t1 = new Thread(new Runnable() {
private int sum = 0;
public void run() {
while (sum < num) {
try {
s1.acquire();
System.out.print("A");
sum ++;
} catch (Exception e) {
e.printStackTrace();
}finally {
s2.release();
}
}
}
});
static Thread t2 = new Thread(new Runnable() {
private int sum = 0;
public void run() {
while (sum < num) {
try {
s2.acquire();
System.out.print("B");
sum ++ ;
} catch (Exception e) {
e.printStackTrace();
}finally {
s3.release();
}
}
}
});
static Thread t3 = new Thread(new Runnable() {
private int sum = 0;
public void run() {
while (sum < num) {
try {
s3.acquire();
System.out.println("C");
sum++;
} catch (Exception e) {
e.printStackTrace();
}finally {
s1.release();
}
}
}
});
}
