复制代码代码如下:
java.util.concurrent.semaphoreをインポートします。
Public Class ThreeThread {
public static void main(string [] args)throws arturnedexception {
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");
スレッドスレッドb = new PrintThread(n、sempb、sempc、 "b");
thread threadc = new PrintThread(n、sempc、sempa、 "c");
threada.start();
threadb.start();
threadc.start();
}
static class printthreadはスレッドを拡張します{
int n;
Semaphore cursemp;
セマフォNEXTSEMP;
文字列名;
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){
試す {
cursemp.acquire();
System.out.println(name);
nextsemp.release();
} catch(arternedexception e){
thread.currentThread()。
}
}
}
}
}