File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes How can I determine when my Threads are complete? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How can I determine when my Threads are complete?" Watch "How can I determine when my Threads are complete?" New topic
Author

How can I determine when my Threads are complete?

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
I recently learned how to use a ThreadPool. This was excellent in my quest
to limit the number of threads running at once. Now I am trying to determine
how I can tell when all of my threads are complete. Here is how I call a thread:


//org.apache.commons.threadpool.ThreadPool
ThreadPool threadPool = new DefaultThreadPool(3);

for (a = 0; a!= someObjects.size(); a++)
{
threadPool.invokeLater(new AdHoc(someObjects.get(a)));
}

My run method for AdHoc:

public void run()
{
// Do something
}


How can I tell when all the threads have been executed?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

If you want to wait until all threads in a list have completed, go through the list and call the join() method of each thread.
soudamini.s shirkhedkar
Greenhorn

Joined: Nov 29, 2005
Posts: 9
We can make use of the methods join() and isAlive() in thread class.

boolean isAlive()
This is used to find out whether a thread is alive(still functioning) or terminated.
A parent thread can call this method to find out the excution status of its child threads before terminating itself. It can choose not to terminate itself if any of its child threads are still functioning.

join()
A call to this method invoked on a thread will wait and not return until the thread has completed. A parent thread can use this method to wait for its child thread to complete before continuing. In other words, parent thread waits for its child thread to join it after completion.

All the best!
Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
you all lost me. What am I calling join on? I can't call it on each new object that implements Runnable, that won't help me (will it) and I cannot call it callit on the ThreadPool.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You will need to get a reference to whatever Thread it is that's running your AdHoc thing. Perhaps the invokeLater() method returns a Thread, or if not then you need some other way of getting that reference. The API documentation should help in that search.

Or perhaps the ThreadPool object itself has a method that returns the number of live threads, or the set of live threads, or something like that. Again you would have to look in the docs to find that out.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18520
    
  40

Originally posted by Anthony Smith:
you all lost me. What am I calling join on? I can't call it on each new object that implements Runnable, that won't help me (will it) and I cannot call it callit on the ThreadPool.


Anthony is correct here. Tasks in a thread pool are just that -- runnable, callable, or whatever, code that the threads in the thread pool will execute. There is no one to one relationship between it and the thread that executes it.

This is why thread pools provide methods that allow you to examine the tasks in the queue, their state, or results return. You will have to look at the API for the threadpool you are using for the answer.

[EDIT] Another possibility is to modify your task to set a variable of some type when it has completed.

Henry
[ December 21, 2005: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
This is ugly but possible ... you can become a Thread factory to the thread pool, and every time you create a new thread keep a reference. (A weak reference would be good so you don't confuse things if the pool ends threads.) Then you'd be able to poll the pool (!) and see which threads are in what state.

Querying the queue stats sounds a lot cleaner. Are you able to shut down any producer threads before you start checking? Otherwise you might find the queue is empty but a producer could put more things in before you can act on that knowledge.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I determine when my Threads are complete?
 
Similar Threads
How to kill runnig thread using java?(not waiting)
Threads and DB Access
ThreadPools and monitoring
How to fix the size of the ThreadPool in an Application ??
Making main program exit on its own