# What is the purpose of using "ForceQueuePolicy"

**URL:** <https://discuss.elastic.co/t/what-is-the-purpose-of-using-forcequeuepolicy/121522>\
**Category:** Elasticsearch\
**Created:** [February 26, 2018, 4:05pm UTC](https://discuss.elastic.co/t/what-is-the-purpose-of-using-forcequeuepolicy/121522 "2018-02-26T16:05:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![GingoBang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gingobang/32/28256_2.png) [@GingoBang](https://discuss.elastic.co/u/GingoBang)\
**Post date:** [February 26, 2018, 4:05pm UTC](https://discuss.elastic.co/t/what-is-the-purpose-of-using-forcequeuepolicy/121522/1 "2018-02-26T16:05:59Z")

</div>

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

```auto
static class ForceQueuePolicy implements XRejectedExecutionHandler {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            try {
                // force queue policy should only be used with a scaling queue
                assert executor.getQueue() instanceof ExecutorScalingQueue;
                executor.getQueue().put(r);
            } 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 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 26, 2018, 4:06pm UTC](https://discuss.elastic.co/t/what-is-the-purpose-of-using-forcequeuepolicy/121522/2 "2018-03-26T16:06:06Z")

</div>

Closed on March 26, 2018, 4:06pm UTC

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