复制代码代码如下:
import java.util.concurrent.semaphore;
clase pública theethread {
public static void main (string [] args) lanza interruptedException {
Semaphore sempa = nuevo semáforo (1);
SEMAPHORE SEMPB = nuevo semáforo (0);
SEMAPHORE SEMPC = nuevo semáforo (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");
thinda.start ();
Threadb.Start ();
threadc.start ();
}
La clase estática PrintThread extiende el hilo {
int n;
Semaphore Cursemp;
Semaphore NEXTSEMP;
Nombre de cadena;
Public PrintThread (int n, Semaphore CursEmp, Semaphore NextSemp, Nombre de cadena) {
N = n;
this.cursemp = cursemp;
this.nextsemp = nextSemp;
this.name = name;
}
public void run () {
para (int i = 0; i <n; ++ i) {
intentar {
cursemp.acquire ();
System.out.println (nombre);
nextSemp.Release ();
} capt (interruptedException e) {
Thread.CurrentThread (). Interrupt ();
}
}
}
}
}