Can java fixedThreadPool be used by multiple threads

I have a web service that does a few small calculations before returning the result. I want to use the ExecutorService

provided Executors.newFixedThreadPool()

as a way to implement the Master-Worker pattern (i.e. Call invokeAll

) and let the thread wait for all results to complete). Ideally, all webservice threads use the same executor service, so they don't all need to create their own thread pool, and they can just use one large pool that uses all the system processing time.

Questions I have with this approach:

  • Is it safe to access the function invokeAll

    from multiple threads.
  • the executor service simply processes the requests sequentially (that is, first all tasks from thread 1 and then those for thread
  • Is there a way to say 10 worker threads and the maximum thread availability depends on the number of incoming requests, so let's say we have 1 request, it uses all 10 threads for that request. If you have 2 requests, it splits them into 5 threads per request, etc.
+1


source to share


3 answers


When running on a Java EE server, you do not have to create threads yourself. I understand that this is not a good situation, so you should research alternatives depending on the application server you are using. If this WebSphere or Weblogic, you should use the WorkManager specification commonj, which provides the desired functionality. There is also an implementation for JBoss .



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

+2


source


I would say don't use invokeAll. Each request calls ExecutorService.submit for each of the subtasks you want to execute in parallel. The thread pool will handle scheduling tasks (which is what it is for!).



And yes, if you use Executors.newFixedThreadPool (), each request is queued, so they are processed sequentially.

+1


source


I think you should use Semaphore and then invokeAll ()

0


source







All Articles