From ad326d15538103a597d58b1833b6b52a5fca39b4 Mon Sep 17 00:00:00 2001 From: Amit Kumar Mondal Date: Fri, 5 May 2023 22:28:11 +0200 Subject: [PATCH] Introduced Thread Pool Optimizations (#1394) fixes #1392 Signed-off-by: Amit Kumar Mondal --- .../zsmartsystems/zigbee/ZigBeeExecutors.java | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/ZigBeeExecutors.java b/com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/ZigBeeExecutors.java index 59ad0a7d9..9545cc59f 100644 --- a/com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/ZigBeeExecutors.java +++ b/com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/ZigBeeExecutors.java @@ -7,11 +7,16 @@ */ package com.zsmartsystems.zigbee; +import static java.util.concurrent.TimeUnit.SECONDS; + import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.atomic.AtomicInteger; import org.slf4j.Logger; @@ -28,6 +33,13 @@ public class ZigBeeExecutors { private static Logger logger = LoggerFactory.getLogger(ZigBeeExecutors.class); + private static final int THREAD_POOL_CORE_THREADS_SIZE = 1; + private static final int THREAD_POOL_KEEP_ALIVE_TIME_IN_SECONDS = 60; + + private ZigBeeExecutors() { + throw new IllegalAccessError("Cannot be instantiated"); + } + /** * Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they * are available, and uses the provided ThreadFactory to create new threads when needed. @@ -59,7 +71,10 @@ public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, // as we cancel a lot we want to get rid of cancelled tasks // even with the penalty of the clean up overhead scheduledThreadPool.setRemoveOnCancelPolicy(true); - + scheduledThreadPool.setKeepAliveTime(THREAD_POOL_KEEP_ALIVE_TIME_IN_SECONDS, SECONDS); + if (corePoolSize > 1) { + scheduledThreadPool.allowCoreThreadTimeOut(true); + } return scheduledThreadPool; } @@ -76,9 +91,7 @@ public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, */ public static ScheduledExecutorService newSingleThreadScheduledExecutor(String name) { // reuse our custom method which configures removal of cancelled tasks - ScheduledExecutorService singleThreadScheduledExecutor = newScheduledThreadPool(1, name); - - return singleThreadScheduledExecutor; + return newScheduledThreadPool(1, name); } /** @@ -94,7 +107,18 @@ public static ScheduledExecutorService newSingleThreadScheduledExecutor(String n * @return the newly created thread pool */ public static ExecutorService newFixedThreadPool(int nThreads, String name) { - return Executors.newFixedThreadPool(nThreads, new ThreadFactoryWithNamePrefix(name)); + // @formatter:off + ThreadPoolExecutor executor = new ThreadPoolExecutor(THREAD_POOL_CORE_THREADS_SIZE, + nThreads, + THREAD_POOL_KEEP_ALIVE_TIME_IN_SECONDS, + SECONDS, + new LinkedBlockingQueue<>(), + new ThreadFactoryWithNamePrefix(name)); + // @formatter:on + if (nThreads > 1) { + executor.allowCoreThreadTimeOut(true); + } + return executor; } /**