Dynamic Thread Pool

后端 未结 1 1411
不思量自难忘°
不思量自难忘° 2020-12-03 23:21

I have a long running process that listens to events and do some intense processing.

Currently I use Executors.newFixedThreadPool(x) to throttle the nu

相关标签:
1条回答
  • 2020-12-04 00:18

    Have a look at below API in ThreadPoolExecutor

    public void setCorePoolSize(int corePoolSize)
    

    Sets the core number of threads. This overrides any value set in the constructor.

    If the new value is smaller than the current value, excess existing threads will be terminated when they next become idle.

    If larger, new threads will, if needed, be started to execute any queued tasks.

    Initialization:

    ExecutorService service = Executors.newFixedThreadPool(5); 
    

    On need basis, resize Thread pool by using below API

    ((ThreadPoolExecutor)service).setCorePoolSize(newLimit);//newLimit is new size of the pool 
    

    Important note:

    If the queue is full, and new value of number of threads is greater than or equal to maxPoolSize defined earlier, Task will be rejected.

    So set values of maxPoolSize and corePoolSize properly.

    0 讨论(0)
提交回复
热议问题