ThreadPoolExecutor : Tasks are getting queued up and not submitted

后端 未结 6 1623
醉酒成梦
醉酒成梦 2021-02-03 11:42

We have a scenario where tasks submitted to ThreadPoolExecutor are long running. When the thread pool is started we start it with core pool size = 5, max pool size = 20 and queu

6条回答
  •  一向
    一向 (楼主)
    2021-02-03 11:55

    The overall situation is like this:

    core pool size = 5,
    max pool size = 20 and 
    queue size of 10
    

    10 tasks are submitted. Out of which

    1. 5 Tasks hanged on I/O => all threads of core pool size are occupied. And hence there is no idle thread.
    2. 5 Tasks are remained . These 5 threads are enqueued to queue since there is no idle thread and the queue can accommodate 10 tasks. These enqueued tasks will not execute until either the queue is full or any of the threads in core pool is free.

    Hence, Your Program is hanged .

    To know more about dynamics of ThreadPoolExecutor watch here . The notable points of this doc is as follows:

    • If fewer than corePoolSize threads are running, the Executor always prefers adding a new thread rather than queuing.
    • If corePoolSize or more threads are running, the Executor always prefers queuing a request rather than adding a new thread.
    • If a request cannot be queued, a new thread is created unless this would exceed maximumPoolSize, in which case, the task will be rejected.

    EDIT
    If you wish to increase core pool size then you can use setCorePoolSize(int corePoolSize) . If you increase the corepoolsize then new threads will, if needed, be started to execute any queued tasks.

提交回复
热议问题