ThreadPoolExecutor : Tasks are getting queued up and not submitted

后端 未结 6 1625
醉酒成梦
醉酒成梦 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条回答
  •  梦毁少年i
    2021-02-03 12:10

    The "variable" sized thread pool feature in the jdk is a bit tricky. Basically, the setup you are using won't work very well for the reasons you outlined. When i want a variable size thread pool, i typically use this setup:

       ThreadPoolExecutor executor = new ThreadPoolExecutor(maxPoolSize, maxPoolSize,
                                                             DEFAULT_THREAD_TIMEOUT, DEFAULT_THREAD_TIMEOUT_UNIT,
                                                             new LinkedBlockingQueue(),
                                                             threadFactory);
        executor.allowCoreThreadTimeOut(true);
    

    this will create a variable sized thread pool which will vary between 1 and maxPoolSize threads. since the core size is the same as the max size, the pool will always prefer adding threads to queuing, thus you will never backup your queue until the number of threads is maxed out.

    UPDATE: As for your hanging tasks problem, if there is an upper limit on the length of a task, you could have a separate manager track the outstanding Future's and cancel them if they are over the max running time. this, of course, assumes your tasks are interruptible.

提交回复
热议问题