/** * 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); } }