• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I implement a thread pool in a java application?

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all:
I need to develop an application in java that will be responsible for receiving simultaneously numerous jabber messages from different sources. Based on the contents and the types of the jabber messages, different methods might need to be executed.
What I would like to see happen is to have a thread pool containing a predefined number of threads and when a jabber messages arrives, upon examining the contents and the type of the message, a thread from the thread pool should be assigned the task of executing the appropriate method for that message and after completing that task, the thread should be added back to the thread pool so that it can process future messages.
Can anyone please provide me with some ideas/thoughts/comments on this matter?
Also, I have briefly looked the util.concurrent package by Doug Lea. Does anyone have any knowledge of how I can apply that package to my needs?
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Creating a ThreadGroup, with predefined no. of threads might help u out right?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Also, I have briefly looked the util.concurrent package by Doug Lea. Does anyone have any knowledge of how I can apply that package to my needs?


Doug is an acknowledged authority and this package is top-rate code. Take a thread pool implementation from there and you're in business.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
U can use the Dougs code,the class called PooledExecutor for thread pool which maintains the pool as its own with implementing sych.
Use method execute(Runnable r);
Param : ur thread class object, it internally calls the passed object run method.
If u wanna queue type of pool u will find one constructor which is having BoundedBuffer as param.
U can use that also.
interruptAll() method to interrupt all the threads.
Then u have to find this thread interruption in ur run code of the thread class with this code.
if(Thread.interrupted) {
//do
} else {
// do
}
 
Shital Kapadia
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Manish
Do you have an example of using the PooledExecutor class using the BoundedBuffer as a parameter. Also, when do you use the queue type of pools?
Thanks,
Shital Kapadia
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sheetal,
you can get the code and necessary documentation from the follwoing url.
http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/index.html.
basu.
 
Shital Kapadia
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Basudev:
The link that you have sent me gives me a 404 http error: no page found. Can you kindly check the url that you have posted at Javaranch and try posting it again.
Thanks a lot,
Shital Kapadia
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just had an extra dot on the end.
http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/index.html
 
V Chauhan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. It was the extra dot at the end.. Thanks Stan.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic