f



Waiting all threads in a fixed thread pool

Hello, may I know how can I wait for all the threads in a fixed threadpool to be completed, without calling shutdownNow?Executor pool = Executors.newFixedThreadPool(nThreads);for(int i = 0; i < 10000;  i++)    pool.execute(new StockHistoryRunnable(code));// blah blah blah//// I would like to wait for all the 10000 task to be completed. Thereis a method named// awaitTermination. However, in order to use the method, I have tofirst call shutdownNow.// I do not want to do so, because I need to re-use the pool later.//Thanks!cheok
0
6/12/2007 12:54:32 AM
comp.lang.java.programmer 52714 articles. 1 followers. Post Follow

4 Replies
651 Views

Similar Articles

[PageSpeed] 53

What aboutfor (Thread t : threadSet) t.join();doThingToDoWhenAllThreadsDone();(I'd be a bit worried about how well Java's threading, or that of yourdeployment OS, will scale up to ten thousand concurrent ones. Or areyou using a substantially smaller pool, say 100, or even just$NUM_CPU_CORES$, with each thread doing jobs from a queue of initially10,000 items and exiting if the queue polls empty?)
0
Twisted
6/12/2007 2:56:36 AM
On Jun 12, 10:56 am, Twisted <twisted...@gmail.com> wrote:> What about>> for (Thread t : threadSet) t.join();> doThingToDoWhenAllThreadsDone();>> (I'd be a bit worried about how well Java's threading, or that of your> deployment OS, will scale up to ten thousand concurrent ones. Or are> you using a substantially smaller pool, say 100, or even just> $NUM_CPU_CORES$, with each thread doing jobs from a queue of initially> 10,000 items and exiting if the queue polls empty?)I dun think I am able to do that so. I have no direct access to thethreads in side the pool. Nor I think it is safe to do so :(
0
yancheng
6/12/2007 3:37:05 AM
On Tue, 12 Jun 2007 01:54:32 +0100, <yancheng.cheok@gmail.com> wrote:> Hello, may I know how can I wait for all the threads in a fixed thread=> pool to be completed, without calling shutdownNow?>> Executor pool =3D Executors.newFixedThreadPool(nThreads);>> for(int i =3D 0; i < 10000;  i++)>     pool.execute(new StockHistoryRunnable(code));>> // blah blah blah> //> // I would like to wait for all the 10000 task to be completed. There> is a method named> // awaitTermination. However, in order to use the method, I have to> first call shutdownNow.> // I do not want to do so, because I need to re-use the pool later.> //>You could use a CountdownLatch  =(http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/CountDownL=atch.html).   =Set the count to the number of tasks and have each of your tasks call  =countDown() when they complete.  The main thread calls await() on the  =latch after submitting the tasks to the thread pool.  This will block  =until the final task has finished.Dan.-- =Daniel Dyerhttp://www.uncommons.org
0
Daniel
6/12/2007 9:02:27 AM
<yancheng.cheok@gmail.com> wrote:> Hello, may I know how can I wait for all the threads in a fixed thread> pool to be completed, without calling shutdownNow?> > Executor pool = Executors.newFixedThreadPool(nThreads);> > for(int i = 0; i < 10000;  i++)>     pool.execute(new StockHistoryRunnable(code));> > // blah blah blah> //> // I would like to wait for all the 10000 task to be completed. There> is a method named> // awaitTermination. However, in order to use the method, I have to> first call shutdownNow.> // I do not want to do so, because I need to re-use the pool later.> //> > Thanks!> > cheokIf you submit to an ExecutorService instead and keep a track of theFuture instances returned, you can poll each one using Future.isDone()and act accordingly.Stan
0
grost
6/16/2007 1:52:51 AM
Reply: