aspose file tools*
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 Spring in Action this week in the Spring 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 am implementing master worker thread pool model. I have workerthreads that increase and decrease. I right now am using
AvailableThreadsList (that can be taken out for processing by Master)
UnAccessableThreadsList (I hold a cache which I use if I need to increase or decrease number of threads in the pool).
My problem:
Each worker thread after it has finished its work has to tell Master that it has finished working. I have a private variable for each worker thread which I set to tell master that it is available.
**Master thread has to go through the whole AvailableThreadList take each thread and see if it is not running and use it to process next request.**
** Master thread has to wait until a thread finishes its work and becomes available incase it can't find one in the list **.
This seem to be a bit unclean for me. I was wondering if there are any other clean solutions to this problem.
one possible solution I thought of is having a list for threads that aren't working, one for threads that are working and one for threads that can't be accessed, but I have to shuffle around a lot to increase and decrease threads (I wouldn't do increase and decrease a lot).
Performance is a big factor in my codes...
Sorry for the long mail, but any comments are appreciated.
Thx
Harsha.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I haven't pooled threads, so this might have some issues, but you need somebody to tell the master that a worker is finished. It looks like there is nobody better for the job than the worker itself. If the worker class has a reference to the master, it can just call a method like:
makeAvailable(this)
Wonder how we make sure the master doesn't assign the worker a new task before the end of the worker method.
You might look at some expert thread pools to see how this is done. Doug Lea's work is on the web, and had a great influence on thread pools planned for some future release of Java.


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
Vinod Chandana
Ranch Hand

Joined: Aug 26, 2003
Posts: 59
I think u can check the current status of the thread. I've done a similar thing.
Cheers,
Vinod.
Originally posted by Harsha Roy:
Hi,
I am implementing master worker thread pool model. I have workerthreads that increase and decrease. I right now am using
AvailableThreadsList (that can be taken out for processing by Master)
UnAccessableThreadsList (I hold a cache which I use if I need to increase or decrease number of threads in the pool).
My problem:
Each worker thread after it has finished its work has to tell Master that it has finished working. I have a private variable for each worker thread which I set to tell master that it is available.
**Master thread has to go through the whole AvailableThreadList take each thread and see if it is not running and use it to process next request.**
** Master thread has to wait until a thread finishes its work and becomes available incase it can't find one in the list **.
This seem to be a bit unclean for me. I was wondering if there are any other clean solutions to this problem.
one possible solution I thought of is having a list for threads that aren't working, one for threads that are working and one for threads that can't be accessed, but I have to shuffle around a lot to increase and decrease threads (I wouldn't do increase and decrease a lot).
Performance is a big factor in my codes...
Sorry for the long mail, but any comments are appreciated.
Thx
Harsha.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Master Worker Thread model