/** * Creates a {@link ScheduledThreadPoolExecutor} with custom name for the threads. * * @param name the prefix to add to the thread name in ThreadFactory. * @return The default thread pool for request timeout and client execution timeout features. */ public static ScheduledThreadPoolExecutor buildDefaultTimeoutThreadPool(final String name) { ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(5, getThreadFactory(name)); safeSetRemoveOnCancel(executor); executor.setKeepAliveTime(5, TimeUnit.SECONDS); executor.allowCoreThreadTimeOut(true); return executor; }
/* CONSTRUCTOR */ public ServiceSensorControl() { // Register this object globally GlobalContext.set(this); // Create the executor service, keep two threads in the pool executorService = new ScheduledThreadPoolExecutor(SensorCollectionOptions.MAIN_EXECUTOR_CORE_POOL); // If feature is available, enable core thread timeout with five seconds if (Build.VERSION.SDK_INT >= 9) { executorService.setKeepAliveTime(MAIN_EXECUTOR_CORE_TIMEOUT, TimeUnit.MILLISECONDS); executorService.allowCoreThreadTimeOut(true); } }
@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); }
{ schedThPoolExec.prestartAllCoreThreads(); schedThPoolExec.setKeepAliveTime(0, TimeUnit.SECONDS); }