复制代码代码如下:
导入java.util.concurrent.semaphore;
公共类三项{
公共静态void main(string [] args)抛出InterruptedException {
信号sempa =新信号(1);
信号sempb =新信号(0);
信号sempc =新信号(0);
int n = 100;
线程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();
}
静态类PrintThread扩展了线程{
int n;
信号函数cursemp;
信号sextsemp;
字符串名称;
public printThread(int n,信号函数cursemp,信号semaphore sempsemp,字符串名称){
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(InterruptedException e){
thread.currentThread()。interrupt();
}
}
}
}
}