Multi threading and pooling resources
Hi
I have a application that i am trying to multi thread.
So I decided to use a fixed thread pool from the new java 5 concurrency package.
I am trying to distribute the work among the threads.
Each thread needs to have access to a number of objects (such as its own connection).
The issue is that its not desirable to create a new connection and other objects for each callable. (The callable takes less than 2 seconds to complete so the overhead is large).
I decided to implement a pool of resources that each thread can access. I add these resources into a blocking queue and pop and push them inside the callable. In the end I get the results and clean up all the resources - close connections, etc.
Pseudo code follows:
//setup up all resources and add them into a queue;
Executor executor = Executors.newFixedThreadPool(n);
for(each thing todo){
futureList.add(executor.submit(new Callable(){
//get object from queue;
try{
//do something;
}finally{
//pop back object into queue;
}
}));
}
for(each result : futureList){
//get result;
}
//clean up all resources in the queue;
Is this the best way to do this? I tried implementing this in my application but the code looks farily horrible in my opinion (despite my best efforts at formatting). Is there a better way to do this?

