我得到了固定數量的線程。我希望每個線程都能一個接一個地運行三個Runnable
。下面是一些僞代碼來解釋:Java - 每個線程的多個Runnables
Thread[] threads = new Thread[4];
for (int i = 0; i < threads.length; i++) {
// Set the first tasks.
threads[i] = new Thread(new FirstRunnable());
threads[i].start();
}
for (int i = 0; i < threads.length; i++)
threads[i].join(); // wait until the first tasks are done
for (int i = 0; i < threads.length; i++) {
// Set the second task.
threads[i].setRunnable(new SecondRunnable());
threads[i].start();
}
for (int i = 0; i < threads.length; i++)
threads[i].join(); // wait until the second tasks are done
...
使用ThreadPool
聽起來方式矯枉過正,特別是因爲我爲首的性能,性能,性能。在Java中實現這個最好的方法是什麼?
這'{0..3} - > 4'螺紋和可運行有 – c00kiemon5ter
[組合兩個Runnable對象]的可能重複(http://stackoverflow.com/questions/4661983/combining-two-runnable -objects) –
@Jon Skeet:在不是所有的FirstRunnable都完成之前,我不想啓動任何'SecondRunnable'。 – ryyst