title: ThreadPool reject tasks when queue is full using RejectedExecutionHandler
body: <p>I know that are lots of questions on this subject but I'm not sure that I found the correct way of doing this.</p>
  <p>What I want: having a ThreadPoolExecutor with a constant number of threads, submit tasks to it. When the internal queue of the ThreadPoolExecutor is full, use a custom RejectedExecutionHandler to reject the last(or first) task submitted to thread pool executor which is in queue(in my example is the last one). I've made some tests and a simple example, but still not sure if this is the right way to do it:</p>
  <pre><code> import java.util.concurrent.*;
  
  class TestThread implements Runnable {
      public int id;
  
      public TestThread(int i) {
          id = i;
      }
  
      @Override
      public void run() {
          try {
              Thread.sleep(20_000);
              System.out.println(&quot; finished task &quot; + id);
          } catch (InterruptedException ex) {
              ex.printStackTrace();
          }
      }
  }
  
  public class Application {
      public static void main(String[] args) {
          LinkedBlockingDeque&lt;Runnable&gt; blockingQueue = new LinkedBlockingDeque&lt;&gt;(10);
  
          ThreadPoolExecutor executor = new ThreadPoolExecutor(2,
                  2,
                  10L,
                  TimeUnit.MINUTES,
                  blockingQueue,
                  (r, executor1) -&gt; {
                      TestThread r1 = (TestThread) blockingQueue.removeLast();
                      System.out.println(&quot;removed &quot; + r1.id);
                  });
          executor.prestartAllCoreThreads();
  
  
          int i = 0;
          while (i &lt;= 15) {
              TestThread testThread = new TestThread(i);
              System.out.println(&quot;submit task &quot; + i);
              executor.execute(testThread);
              System.out.println(&quot;queue size &quot; + blockingQueue.stream().count());
              i++;
              try {
                  Thread.sleep(500);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
  
          i = 0;
          while (i &lt;= 5) {
              TestThread testThread1 = new TestThread(i);
              System.out.println(&quot;submit task &quot; + i);
              executor.execute(testThread1);
              System.out.println(&quot;queue size &quot; + blockingQueue.stream().count());
              i++;
              try {
                  Thread.sleep(1000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
  
          try {
              executor.shutdown();
              executor.awaitTermination(10L, TimeUnit.SECONDS);
          } catch (InterruptedException e) {
              executor.shutdownNow();
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  <p>Do I miss something here?</p>
  <p>LE: do I need to lock the queue when removing the element from it?</p>