This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes How to Monitor Threads? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to Monitor Threads?" Watch "How to Monitor Threads?" New topic
Author

How to Monitor Threads?

Ravi Sathish
Ranch Hand

Joined: Feb 26, 2002
Posts: 131
Ranchers,

I need a Monitor Thread monitoring a bunch of Worker Threads. Now what is the best to implement Monitoring?

Option 1> Monitor is constantly iterating a Thread collection to find out who's running... If a Worker completed a task successfully then do something?

Option 2> Monitor is sleeping waiting to be notified by a Worker Thread? If so is there a way to find out which Worker Thread notified?

Could please suggest better design solutions?

TIA
Ravi
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
In your MonitorThread, create a synchronized workerCompleted() method that takes the WorkerThread that is done with its task. The method should add the WT to MT's List of completed WTs. It then wakes up the MT using notify() or interrupt() or some other signal so that it can process the newly completed WT.

You should check out Doug Lea's Concurrent library for some very handy threading classes.
Ravi Sathish
Ranch Hand

Joined: Feb 26, 2002
Posts: 131
Dave,

In your MonitorThread, create a synchronized workerCompleted() method that takes the WorkerThread that is done with its task. The method should add the WT to MT's List of completed WTs. It then wakes up the MT using notify() or interrupt() or some other signal so that it can process the newly completed WT.

You should check out Doug Lea's Concurrent library for some very handy threading classes.


I'm still not getting how to implement long running Monitors efficiently.

I have implemented this much so far:

1> workers complete their task and add themselves into a hashtable by invoking Monitor's workerCompleted() method (this is synchronized) ofcourse;
after this notifies itself;

2> Now monitor knows its a worker has completed its task; So does some work() goes back to waiting state.

Is this the most efficient implementation..

Viz, Doug Leas util.concurrent package; Channel looks most promising... I need to do read more on how its applicable to currnet scenario..

I would appreciate future suggestions

Ravi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Monitor Threads?
 
Similar Threads
Removing Thread blocks
Thread
Threads in Servlets
NX: URLYBird / my approach of the reading problem
Thread problem please help