I have a Java Application which handles different region for example 10 regions. Each region have different number of tasks.
I encounter a problem is that one particular region (Region A
) have lots of tasks and the processing time of each task for that region is very long. Therefore, if I use a Single Thread Pool
, and tasks of all region will be submitted to the Single Thread Pool
, tasks of Region A
will block other region 's tasks. Tasks of other region can only wait in the block queue until the tasks of Region A
are finished.
I want to achieve if there are 5000 tasks of Region A
submitted to Single Thread Pool
, and each task is processing with a long time, other region tasks can be processed in parallel without being blocked by tasks of Region A
.
I have a requirement which cannot create too much thread for the Single Thread Pool
, and also newCachedThreadPool()
is not recommended to use.
What my approach is to create 1 Thread Pool for each region, which will create 10 Thread Pool in total. But I do not know if there are any problems regarding multiple Thread Pool and if there are any other proper way to achieve it.
CodePudding user response:
The processing capacity of running threads is always limited by the number of threads the hardware's CPU provides - keep that in mind when allocating threads to thread pools.
Use newFixedThreadPool(int nThreads)
for each region.
If your processing is mostly compute intensive, pick smaller sizes; perhaps newFixedThreadPool(3)
for region A and newFixedThreadPool(1)
for the others.
Chose larger numbers if your processing spends a lot of time waiting for processing external to the thread (eg web or db calls).
Experiment to see what provides acceptable liveliness.