From 7af87f37d073f5a931404b60b9a909e4348229c1 Mon Sep 17 00:00:00 2001 From: Amit Kumar Mondal Date: Tue, 25 Apr 2023 11:30:35 +0200 Subject: [PATCH] Introduced Thread Pool Optimizations fixes #1392 Signed-off-by: Amit Kumar Mondal --- .../zsmartsystems/zigbee/ZigBeeExecutors.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 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 59ad0a7d92..109c17c660 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,15 @@ */ 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.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 +32,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,6 +70,8 @@ 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.allowCoreThreadTimeOut(true); + scheduledThreadPool.setKeepAliveTime(THREAD_POOL_KEEP_ALIVE_TIME_IN_SECONDS, SECONDS); return scheduledThreadPool; } @@ -76,9 +89,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 +105,16 @@ 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 SynchronousQueue<>(), + new ThreadFactoryWithNamePrefix(name)); + // @formatter:on + executor.allowCoreThreadTimeOut(true); + return executor; } /**