• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Shital Kapadia
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • 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?
 
Anand Ko
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Creating a ThreadGroup, with predefined no. of threads might help u out right?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Manish Malhotra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
V Chauhan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Yes. It was the extra dot at the end.. Thanks Stan.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!