package com.continuuity.internal.app.runtime.schedule;

import org.quartz.SchedulerConfigException;
import org.quartz.spi.ThreadPool;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Executor based ThreadPool used in quartz scheduler.
 */

public final class ExecutorThreadPool implements ThreadPool {

  private static final int CORE_POOL_SIZE = 1;
  private static final int MAX_THREAD_POOL_SIZE = 20;
  private static final long KEEP_ALIVE_TIME = 60;

  private ThreadPoolExecutor executor;

  @Override
  public boolean runInThread(Runnable runnable) {
    executor.execute(runnable);
    return true;
  }

  @Override
  public int blockForAvailableThreads() {
    //num of threads available
    return executor.getMaximumPoolSize() - executor.getActiveCount();
  }


  @Override
  public void initialize() throws SchedulerConfigException {
    executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_THREAD_POOL_SIZE,
                                      KEEP_ALIVE_TIME, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
  }

  @Override
  public void shutdown(boolean waitForJobsToComplete) {
    executor.shutdown();
  }


  @Override
  public int getPoolSize() {
    return executor.getPoolSize();
  }


  @Override
  public void setInstanceId(String schedInstId) {
    //no-op
  }

  @Override
  public void setInstanceName(String schedName) {
    //noop
  }
}

@@ -156,31 +156,30 @@
 current.
-SynchronousQueu
+ExecutorServic
 e;%0Aimpor
@@ -205,64 +205,17 @@
 ent.
-ThreadPoolExecutor;%0Aimport java.util.concurrent.TimeUnit
+Executors
 ;%0A%0A/
@@ -344,55 +344,8 @@
  %7B%0A%0A
-  private static final int CORE_POOL_SIZE = 1;%0A
   pr
@@ -394,9 +394,10 @@
 E = 
-2
+50
 0;%0A 
@@ -409,78 +409,86 @@
 ate 
-static final long KEEP_ALIVE_TIME = 60;%0A%0A  private ThreadPoolE
+final ExecutorService executor;%0A%0A  public ExecutorThreadPool() %7B%0A    e
 xecutor 
 exec
@@ -483,25 +483,73 @@
 xecutor 
-e
+= E
 xecutor
-;
+s.newFixedThreadPool(MAX_THREAD_POOL_SIZE);%0A  %7D
 %0A%0A  @Ove
@@ -722,365 +722,195 @@
   //
-num of threads available%0A    return executor.getMaximumPoolSize() - executor.getActiveCount();%0A  %7D%0A%0A%0A  @Override%0A  public void initialize() throws SchedulerConfigException %7B%0A    executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_THREAD_POOL_SIZE,%0A                                      KEEP_ALIVE_TIME, TimeUnit.SECONDS, new SynchronousQueue%3CRunnable%3E());
+Always accept new work. Additional runnables will be in the executor queue.%0A    return MAX_THREAD_POOL_SIZE;%0A  %7D%0A%0A%0A  @Override%0A  public void initialize() throws SchedulerConfigException %7B
 %0A  %7D

Use executor service