What is the purpose of using "ForceQueuePolicy"

(Ruijie) #1

Here is the code:
It will be used if the ThreadPoolExecutor instance reject the task submitted.

static class ForceQueuePolicy implements XRejectedExecutionHandler {
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            try {
                // force queue policy should only be used with a scaling queue
                assert executor.getQueue() instanceof ExecutorScalingQueue;
            } catch (final InterruptedException e) {
                // a scaling queue never blocks so a put to it can never be interrupted
                throw new AssertionError(e);
  1. What we want for the SCALING executor is that to control the size of threads we created better, we only hold small size of threads if number of tasks is small, we can create maximum number of threads quickly that do not need to wait for the Queue to be full. By using the ExecutorScalingQueue we can make it.

  2. The rejection handler will be invoked when:

  • the executor had been shut down
  • failed to create a worker to execute the task

Hope to understand this point and thanks for answering :slight_smile:

(system) #2

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.