Waiting all threads in a fixed thread pool

Y

yancheng.cheok

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!

cheok
 
T

Twisted

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?)
 
Y

yancheng.cheok

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 the
threads in side the pool. Nor I think it is safe to do so :(
 
D

Daniel Dyer

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.
//

You could use a CountdownLatch
(http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/CountDownLatch.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.
 
S

Stan

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!

cheok

If you submit to an ExecutorService instead and keep a track of the
Future instances returned, you can poll each one using Future.isDone()
and act accordingly.

Stan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top