我一直在瀏覽啓動三個線程的程序,並打印出相應的值,使得T3
先執行,然後執行T1
線程,最後執行T2
線程。以下是該計劃。關於倒數鎖存器的實現
我只是想知道,如果你們可以幫助轉換這個程序就倒計時閂鎖,因爲我想用這種機制來開發它,或者它也可以通過計數信號量來完成。
從answer to this related question:
public class Test {
static class Printer implements Runnable {
private final int from;
private final int to;
private Thread joinThread;
Printer(int from, int to, Thread joinThread) {
this.from = from;
this.to = to;
this.joinThread = joinThread;
}
@Override
public void run() {
if(joinThread != null) {
try {
joinThread.join();
} catch (InterruptedException e) { /* ignore for test purposes */ }
}
for (int i = from; i <= to; i++) {
System.out.println(i);
}
}
}
public static void main(String[] args) throws InterruptedException {
Thread T3 = new Thread(new Printer(10, 15, null));
Thread T1 = new Thread(new Printer(1, 5, T3));
Thread T2 = new Thread(new Printer(6, 10, T1));
T1.start();
T2.start();
T3.start();
}
}
@LouisWasserman,可能不是很多,因爲他從http://stackoverflow.com/questions/15029703/regarding-starting-the-threads-on-a-condition中給出的答案中複製了它。但你沒有太多幫助,因爲這是我看到你在任何地方發帖的唯一評論。 – 2013-02-23 05:19:47