Example #1
0
  /**
   * Creates a thread pool that can schedule commands to run after a given delay, or to execute
   * periodically.
   *
   * @param corePoolSize Number of threads to keep in the pool, even if they are idle.
   * @param threadName Part of thread name that would be used by thread factory.
   * @return Newly created scheduled thread pool.
   */
  private static ScheduledExecutorService newScheduledThreadPool(
      int corePoolSize, final String threadName) {
    ScheduledExecutorService srvc =
        Executors.newScheduledThreadPool(
            corePoolSize,
            new ThreadFactory() {
              @Override
              public Thread newThread(Runnable r) {
                Thread thread =
                    new Thread(r, String.format("%s-%d", threadName, THREAD_CNT.getAndIncrement()));

                thread.setDaemon(true);

                return thread;
              }
            });

    ScheduledThreadPoolExecutor executor = (ScheduledThreadPoolExecutor) srvc;

    // Setting up shutdown policy.
    executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
    executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);

    return srvc;
  }
 @Override
 protected void serviceInit(Configuration conf) throws Exception {
   ThreadFactory tf = new ThreadFactoryBuilder().setNameFormat("DeletionService #%d").build();
   if (conf != null) {
     sched =
         new ScheduledThreadPoolExecutor(
             conf.getInt(
                 YarnConfiguration.NM_DELETE_THREAD_COUNT,
                 YarnConfiguration.DEFAULT_NM_DELETE_THREAD_COUNT),
             tf);
     debugDelay = conf.getInt(YarnConfiguration.DEBUG_NM_DELETE_DELAY_SEC, 0);
   } else {
     sched = new ScheduledThreadPoolExecutor(YarnConfiguration.DEFAULT_NM_DELETE_THREAD_COUNT, tf);
   }
   sched.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
   sched.setKeepAliveTime(60L, SECONDS);
   if (stateStore.canRecover()) {
     recover(stateStore.loadDeletionServiceState());
   }
   super.serviceInit(conf);
 }