wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Master Worker Thread Model Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Master Worker Thread Model" Watch "Master Worker Thread Model" New topic
Author

Master Worker Thread Model

Harsha Roy
Greenhorn

Joined: Oct 08, 2003
Posts: 2
Hi,
I have implemented a Master Worker thread model. My problem now is say my master delegates the work to a Worker Thread from worker thread pool. The worker thread fails. How should I be handling this. In the sense, should the master thread check if worker thread has completed?
My concern is its not often that the worker thread fails, and if I continually check from the master thread, I'm wasting Masters time doing that.
Any comments are apperciated.
Thanks
Harsha.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
I have implemented a Master Worker thread model. My problem now is say my master delegates the work to a Worker Thread from worker thread pool. The worker thread fails. How should I be handling this. In the sense, should the master thread check if worker thread has completed?
Since the exception cannot be propagated from the thread to whoever started the thread, what you could do is set up a messaging mechanism. For example, you you can pass a Master Worker reference to the Worker Thread, and when Worker Thread throws an exception or completes, it can notify the Master worker by calling a public method of the Master Worker (a callback method). Alternatively, you can create an instance of some Messenger class which will act as a mediator between the Master and the Worker: when the worker completes (or throws an exception), it will call the appropriate method of the Messenger that will in turn notify the Master.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I just read some code (source for www.fitNesse.org by Robert Martin) where the Runnable class for the worker thread is an inner class of the Master. Thus it has access to Master's instance variables which eliminates all the messaging problems pretty neatly. The inner class is just a shell with a run() method that calls another class that does the real work. I haven't decided yet if this is a pleasing design. It kinda troubles me, but then again it's Uncle Bob.
Another feature: The workers put themselves in a collection of currently running threads at start and take themselves out at end. When a request comes in to shut down the master, the master does join() on one thread at a time until the running thread pool goes empty. A nice graceful shutdown, I think.


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
 
wood burning stoves
 
subject: Master Worker Thread Model