Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Waiting all threads in a fixed thread pool

Thread view: 
yancheng.cheok@gmail.com - 12 Jun 2007 01:54 GMT
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
Twisted - 12 Jun 2007 03:56 GMT
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?)
yancheng.cheok@gmail.com - 12 Jun 2007 04:37 GMT
> What about
>
[quoted text clipped - 6 lines]
> $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 :(
Daniel Dyer - 12 Jun 2007 10:02 GMT
> Hello, may I know how can I wait for all the threads in a fixed thread
> pool to be completed, without calling shutdownNow?
[quoted text clipped - 12 lines]
> // 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.

Signature

Daniel Dyer
http://www.uncommons.org

Stan - 16 Jun 2007 02:52 GMT
> Hello, may I know how can I wait for all the threads in a fixed thread
> pool to be completed, without calling shutdownNow?
[quoted text clipped - 16 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.