views:

931

answers:

3

I have a webservice that does multiple small calculations before returning the result. I want to use the ExecutorService provided by Executors.newFixedThreadPool() as a way to implement the Master - Worker pattern (ie. call invokeAll and let the thread wait for all results to finish). Ideally all webservice threads use the same executor service so that they don't all have to create their own thread pool and they can just share one big pool that uses up all of the system's processing time.

Questions I have with this approach:

  • is it safe to access the invokeAll function from multiple threads.
  • will the executor service just handle the requests sequentially (ie. first all the tasks from thread 1, then those for thread
  • is there a way to have say 10 worker threads and have the maximum of threads available depend on the number of requests comming in, so say we have 1 request, it uses all 10 threads for that request. If you have 2 requests, it splits them 5 threads per request etc.
+1  A: 

I would say don't use invokeAll. Have each request call ExecutorService.submit for each one of the subtasks you want performed in parallel. The thread pool will handle scheduling of tasks (that's what its designed for!).

And yes, if you use Executors.newFixedThreadPool() each request gets placed in a queue, so they're processed sequentially.

bajafresh4life
A: 

I think you should use Semaphore rather then invokeAll()

Dennis Cheung
+1  A: 

When operating in a JEE server, you should not create threads on your own. I realize that this is not great situation, so you should investigate alternatives depending on the app server you are using. If it is WebSphere or Weblogic, you should use the WorkManager of the commonj spec, which provides the exact functionality you want. There is also an implementation for JBoss as well.

You should look at creating your own threads in a managed environment as the last resort.

Robin