复制代码代码如下:
استيراد java.util.concurrent.semaphore ؛
الطبقة العامة threethread {
رميات الفراغ الثابتة العامة (سلسلة [] args)
semaphore sempa = semaphore new (1) ؛
semaphore sempb = semaphore new (0) ؛
semaphore sempc = semaphore (0) ؛
int n = 100 ؛
threada 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 () ؛
}
طباعة الفئة الثابتة يمتد مؤشر الترابط {
int n ؛
Smaphore Cursemp.
semaphore nextsemp ؛
اسم السلسلة
printthread العامة (int n ، semaphore cursemp ، semaphore nextsemp ، اسم السلسلة) {
ن = ن ؛
this.cursemp = cursemp ؛
this.nextsemp = nextsemp ؛
this.name = name ؛
}
تشغيل الفراغ العام () {
لـ (int i = 0 ؛ i <n ؛ ++ i) {
يحاول {
cursemp.acquire () ؛
System.out.println (name) ؛
nextsemp.release () ؛
} catch (InterruptedException e) {
thread.currentThRead (). interrupt () ؛
}
}
}
}
}