@Test (timeout = 120000)
  public void testPriorityBasedExecutor() throws Exception {
    List<Priority> priorityList = Collections.synchronizedList(new ArrayList<Priority>());
    ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("priority-exec-thread-%d").build();
    ThreadPoolExecutor threadPoolExecutor = new PriorityBasedThreadPoolExecutor(2, 2, 0L,
        TimeUnit.MILLISECONDS, factory);
    Future<?> future1 = threadPoolExecutor.submit(
        RunnableWithPriority.get(new DummyRunnable(priorityList, Priority.LOW), Priority.LOW));
    Future<?> future2 = threadPoolExecutor.submit(
        RunnableWithPriority.get(new DummyRunnable(priorityList, Priority.LOW), Priority.LOW));
    Future<?> future3 = threadPoolExecutor.submit(
        RunnableWithPriority.get(new DummyRunnable(priorityList, Priority.LOW), Priority.LOW));
    Future<?> future4 = threadPoolExecutor.submit(
        RunnableWithPriority.get(new DummyRunnable(priorityList, Priority.LOW), Priority.LOW));
    Future<?> future5 = threadPoolExecutor.submit(
        RunnableWithPriority.get(new DummyRunnable(priorityList, Priority.NORMAL), Priority.NORMAL));
    Future<?> future6 = threadPoolExecutor.submit(
        RunnableWithPriority.get(new DummyRunnable(priorityList, Priority.NORMAL), Priority.NORMAL));
    Future<?> future7 = threadPoolExecutor.submit(
        RunnableWithPriority.get(new DummyRunnable(priorityList, Priority.HIGH), Priority.HIGH));
    Future<?> future8 = threadPoolExecutor.submit(
        RunnableWithPriority.get(new DummyRunnable(priorityList, Priority.HIGH), Priority.HIGH));
    List<Priority> expectedPriorityList = Lists.newArrayList(Priority.LOW, Priority.LOW);
    while (priorityList.size() < 2) {
      Thread.sleep(100);
    }
    Assert.assertEquals(expectedPriorityList, priorityList);
    future1.cancel(true);
    future2.cancel(true);
    expectedPriorityList.addAll(Lists.newArrayList(Priority.HIGH, Priority.HIGH));
    while (priorityList.size() < 4) {
      Thread.sleep(100);
    }
    Assert.assertEquals(expectedPriorityList, priorityList);
    future7.cancel(true);
    future8.cancel(true);
    expectedPriorityList.addAll(Lists.newArrayList(Priority.NORMAL, Priority.NORMAL));
    while (priorityList.size() < 6) {
      Thread.sleep(100);
    }
    Assert.assertEquals(expectedPriorityList, priorityList);
    future5.cancel(true);
    future6.cancel(true);
    expectedPriorityList.addAll(Lists.newArrayList(Priority.LOW, Priority.LOW));
    while (priorityList.size() < 8) {
      Thread.sleep(100);
    }
    Assert.assertEquals(expectedPriorityList, priorityList);
    future3.cancel(true);
    future4.cancel(true);
    threadPoolExecutor.shutdownNow();
  }
Test Priority based thread pool executor.
test/com/linkedin/drelephant/priorityexecutor/PriorityBasedThreadPoolExecutorTest.java