Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Master Worker Thread Model

 
Harsha Roy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic