aspose file tools*
The moose likes Threads and Synchronization and the fly likes help in having 'n' BlockedQueues and 'm' threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "help in having Watch "help in having New topic
Author

help in having 'n' BlockedQueues and 'm' threads

Vijaishanker bala
Ranch Hand

Joined: Sep 08, 2005
Posts: 82
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.

Thanks

Vijai


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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Vijaishanker bala
Ranch Hand

Joined: Sep 08, 2005
Posts: 82
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.

Thanks
Vijai
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help in having 'n' BlockedQueues and 'm' threads
 
Similar Threads
a complex one-to-one problem
OOP 4 - Lookup
JVM is hang and throughing HTTP 500 Error [Locking]
problems comparing two people for a dating agency program
2 phase commit (commit phase timeout)