复制代码代码如下 :
java.util.concurrent.semaphore import;
공개 클래스 Threethread {
public static void main (string [] args)은 InterruptedException {
세마포어 SEMPA = New Semaphore (1);
세마포어 SEMPB = 새로운 세마포어 (0);
세마포어 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는 스레드 {{
int n;
세마포어 cursemp;
세마포어 Nextsem;
문자열 이름;
public printthread (int n, semaphore cursemp, semaphore nextsemp, 문자열 이름) {
n = n;
this.cursemp = cursemp;
this.nextsemp = nextsemp;
this.name = 이름;
}
public void run () {
for (int i = 0; i <n; ++ i) {
노력하다 {
cursemp.acquire ();
System.out.println (이름);
nextsemp.release ();
} catch (InterruptedException e) {
thread.currentthread (). 인터럽트 ();
}
}
}
}
}