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.
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.
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