→ swpoker: 我想是你程式寫法的問題~先把你CODE放上來吧 11/12 14:53
final CountDownLatch beginA = new CountDownLatch(1);
final CountDownLatch end = new CountDownLatch(2);
final CountDownLatch endE = new CountDownLatch(1);
final ExecutorService exec = Executors.newFixedThreadPool(3);
Runnable runA = new Runnable() {
public void run() {
try {
System.out.println("Thread A 開始..");
for(int i = 0; i < 5; i++) {
Thread.sleep(1000);
System.out.println("Thread A 執行..");
}
System.out.println("Thread A 即將結束..");
Thread.sleep((long) (Math.random() * 10000));
} catch (InterruptedException e) {
} finally {
beginA.countDown();
}
}
};
Runnable runB = new Runnable() {
public void run() {
try {
beginA.await();
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 即將結束..");
Thread.sleep((long) (Math.random() * 10000));
} catch (InterruptedException e) {
} finally {
end.countDown();
}
}
};
Runnable runC = new Runnable() {
public void run() {
try {
beginA.await();
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 即將結束..");
Thread.sleep((long) (Math.random() * 10000));
} catch (InterruptedException e) {
} finally {
end.countDown();
}
}
};
Runnable runE = new Runnable() {
public void run() {
try {
end.await();
System.out.println("Thread E 開始..");
for(int i = 0; i < 5; i++) {
Thread.sleep(1000);
System.out.println("Thread E 執行..");
}
System.out.println("Thread E 即將結束..");
Thread.sleep((long) (Math.random() * 10000));
} catch (InterruptedException e) {
} finally {
endE.countDown();
}
}
};
exec.submit(runA);
exec.submit(runB);
exec.submit(runC);
exec.submit(runE);
try {
endE.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
exec.shutdown();
※ 編輯: PTTCATKING (42.66.159.143), 11/12/2014 14:59:15
這樣寫大概就是 A跑完後開始跑 B跟C ,C跑完跑D ,BCD都跑完最後才跑E
只是實際上的需求是要用資料庫裡 兩個編號來決定優先順序
而修改第兩個編號可能每隻程式的執行順序就會改變
跪求神手指點名燈 T_T
※ 編輯: PTTCATKING (42.66.159.143), 11/12/2014 17:40:05
→ ssccg: 有一定要先開跑再wait再那邊的理由嗎? 11/12 20:12
→ ssccg: 前一個跑完後select出所有該接它後面跑的來跑不行? 11/12 20:14
推 yyc1217: 有點類似無限狀態機(FSM)的感覺 11/12 21:06
→ yyc1217: 另外用一個singleton的manager來做state之間的管理? 11/12 21:07
→ luoqr: 跑完的去通知在等他的那些人開動不行嗎? :S 11/12 21:24
→ carylorrk: FSM(finite state mahine)不是有限狀態機嗎 XDD 11/13 04:06
推 yyc1217: 對不起記錯了..... 11/13 09:22