Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Monitor Threads?

 
Ravi Sathish
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic