In my multithreaded application I am having, say for example 4 BlockedQueues and each of these queues are fed indirectly by a JMS Queue. Each of these 4 BlockedQueues will hold objects that implement interface Runnable. Now I want to have 3 WorkerThreads that will take those objects from the BlockedQueues and run it in them. I have searched for a while regarding tutorials or information in this area but in vain. Since I am new to multithreading I would appreciate if anyone could suggest a source of information or a design pattern which can be used for this problem.
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." <br />Linus Torvalds
I'm not clear on the relationship between queues and workers. Can any worker take from any queue? I'm going to assume "yes" for now.
What comes to mind is to get all the Runnables into one queue so a regular threadpool executor can pull them out and give them to the worker threads.
Can you get all four JMS listeners to put Runnables into a single queue?
If not, can you imagine four threads pulling from the four queues and writing to one new queue, then the threadpool passing work from that queue to the three worker threads? We're up to 7 new threads, but the JVM can probably handle hundreds so I'm not too worried. It's not pretty but it oughtta do the job.
I'd try to avoid one thread polling all four input queues. I just wouldn't be comfortable that I could get work out quickly and at a fair balance without burning a lot of CPU on the polling.
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
Joined: Sep 08, 2005
thanks for the reply Stan, I will elaborate on the problem I have, it is a print server application. A JMS queues will hold the print jobs coming in. Each of these print jobs have a printer. So I sort the print jobs coming in,according to the printer and put them in BlockingQueues. Now I am allowed to create 'n' threads, say 4 threads, but the number of BlockingQueues may or may not be equal to 4. Now I need to take these jobs from the BlockingQueue and assign it to each of the 4 threads. But when I assign a job from one of the BlockingQueues, then that will be locked, until the previously assigned job is finished by any of the 4 threads.Hope this description would help.
Joined: Jan 29, 2003
If you have a queue per printer and you don't want two threads trying to use the same printer at the same time, then a thread per queue sounds like a natural solution. Each thread does a very simple get job from queue, send job to printer, repeat forever.
I can't guess why you have a requirement to have anything other than one thread per queue+printer. Why does that come up?