【发布时间】:2013-02-08 11:05:29
【问题描述】:
我一直在执行启动三个线程并打印它们对应的值的程序,这样首先执行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,可能不是很多,因为他从stackoverflow.com/questions/15029703/… 给他的答案中复制了它。但你并没有提供太多帮助,因为这几乎是我看到你在任何地方发布的唯一评论。
标签: java multithreading java.util.concurrent