[问题] 想请问关于JOIN的写法

楼主: PTTCATKING (怀念美国猫王)   2014-11-10 14:00:17
现在一个流程是这样
Thread A 执行完成之后,分成两条Thread,同时执行 B跟 C
然后C这条线执行完后,会再执行D
等到B这条线跟 C&D这条线两条都跑完后,才开始执行E
如果像是我以下这种写法,算是 B跟 CD跑完之后,才开始跑E吗
那一开始起头的 A 我要怎么写呢
public void execute(TaskExecutionContext executor) throws RuntimeException {
System.out.println("Thread E ");
Thread threadB = new Thread(new Runnable() {
public void run() {
try {
System.out.println("Thread B 开始..");
for(int i = 0; i < 5; i++) {
Thread.sleep(1000);
System.out.println("Thread B 执行..");
}
System.out.println("Thread B 即将结束..");
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
});
Thread threadC = new Thread(new Runnable() {
public void run() {
try {
System.out.println("Thread C 开始..");
for(int i = 0; i < 5; i++) {
Thread.sleep(1000);
System.out.println("Thread C 执行..");
}
System.out.println("Thread C 即将结束..");
System.out.println("Thread D 开始..");
for(int i = 0; i < 5; i++) {
Thread.sleep(1000);
System.out.println("Thread D 执行..");
}
System.out.println("Thread D 即将结束..");
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
});
threadB.start();
threadC.start();
try {
// Thread B 加入 Thread A
threadB.join();
threadC.join();
}
catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread E 执行");
for (int i = 1; i <= 30; i++) {
System.out.println("Task 1 says: " + i + executor);
executor.setStatusMessage("第一个任务执行到第 " + i + " 个");
executor.setCompleteness(i / 30D);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
;
}
executor.pauseIfRequested();
if (executor.isStopped()) {
break;
}
}
}
格式可能有点跑掉,这是 让E等待 B跟CD的写法
怎么写成从A开始呢
因为 E 就是主程序了说..
作者: swpoker (swpoker)   2014-11-10 15:27:00
concurrent不用吗~不要再用thread了拉
作者: nOhiTmE   2014-11-10 20:50:00
如果真的要abcde的话,就用两个countdown latch。cd共用一个,bde共用一个。
楼主: PTTCATKING (怀念美国猫王)   2014-11-11 15:26:00
感谢楼上两位,研究楼上们的推荐,很顺利的完成了感谢高手的意见提供

Links booklink

Contact Us: admin [ a t ] ucptt.com