• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I determine when my Threads are complete?

 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21155
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21155
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21227
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic