复制代码代码如下:
импортировать java.util.concurrent.semaphore;
открытый класс тройной {
Public Static void Main (String [] args) бросает прерывание
Semaphore sempa = новый Semaphore (1);
Semaphore sempb = новый Semaphore (0);
Semaphore sempc = new Semaphore (0);
int n = 100;
Thread Threada = new PrintThread (n, sempa, sempb, "a");
Thread Thinkb = new PrintThread (N, SEMPB, SEMPC, "B");
Thread Threadc = new PrintThread (N, SEMPC, SEMPA, "C");
threada.start ();
ThintB.Start ();
ThreadC.Start ();
}
Static Class PrintThread Extends Thread {
int n;
Семфор Cursemp;
Semaphore nextsemp;
String name;
public printthread (int n, semaphore cursemp, semaphore nextsemp, string name) {
N = n;
this.cursemp = cursemp;
this.nextemp = nextsemp;
this.name = name;
}
public void run () {
для (int i = 0; i <n; ++ i) {
пытаться {
cursemp.acquire ();
System.out.println (имя);
nextSemp.release ();
} catch (прерванная экспрессия e) {
Thread.currentThread (). Enterrupt ();
}
}
}
}
}