Copy Code code as follows:
Import Java.util.concurrent.Semaphore;
public class Threethread {
public static void Main (string[] args) throws Interruptedexception {
Semaphore Sempa = new semaphore (1);
Semaphore SEMPB = new semaphore (0);
Semaphore SEMPC = new semaphore (0);
int n=100;
Thread Threada = new Printthread (N, Sempa, SEMPB, "A");
Thread threadb = new Printthread (N, SEMPB, SEMPC, "B");
Thread THREADC = new Printthread (N, SEMPC, Sempa, "C");
Threada.start ();
Threadb.start ();
Threadc.start ();
}
Static class Printthread extends thread{
int N;
Semaphore cursemp;
Semaphore nextsemp;
String name;
public Printthread (int n, Semaphore cursemp, Semaphore nextsemp, String name) {
n = n;
This.cursemp = cursemp;
This.nextsemp = nextsemp;
THIS.name = name;
}
public void Run () {
for (int i = 0; i < N; ++i) {
try {
Cursemp.acquire ();
SYSTEM.OUT.PRINTLN (name);
Nextsemp.release ();
catch (Interruptedexception e) {
Thread.CurrentThread (). interrupt ();
}
}
}
}
}