Skip to content

Commit ad326d1

Browse files
authored
Introduced Thread Pool Optimizations (#1394)
fixes #1392 Signed-off-by: Amit Kumar Mondal <admin@amitinside.com>
1 parent 51d6503 commit ad326d1

1 file changed

Lines changed: 29 additions & 5 deletions

File tree

com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/ZigBeeExecutors.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
*/
88
package com.zsmartsystems.zigbee;
99

10+
import static java.util.concurrent.TimeUnit.SECONDS;
11+
1012
import java.util.concurrent.ExecutorService;
1113
import java.util.concurrent.Executors;
14+
import java.util.concurrent.LinkedBlockingQueue;
1215
import java.util.concurrent.ScheduledExecutorService;
1316
import java.util.concurrent.ScheduledThreadPoolExecutor;
17+
import java.util.concurrent.SynchronousQueue;
1418
import java.util.concurrent.ThreadFactory;
19+
import java.util.concurrent.ThreadPoolExecutor;
1520
import java.util.concurrent.atomic.AtomicInteger;
1621

1722
import org.slf4j.Logger;
@@ -28,6 +33,13 @@ public class ZigBeeExecutors {
2833

2934
private static Logger logger = LoggerFactory.getLogger(ZigBeeExecutors.class);
3035

36+
private static final int THREAD_POOL_CORE_THREADS_SIZE = 1;
37+
private static final int THREAD_POOL_KEEP_ALIVE_TIME_IN_SECONDS = 60;
38+
39+
private ZigBeeExecutors() {
40+
throw new IllegalAccessError("Cannot be instantiated");
41+
}
42+
3143
/**
3244
* Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they
3345
* are available, and uses the provided ThreadFactory to create new threads when needed.
@@ -59,7 +71,10 @@ public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize,
5971
// as we cancel a lot we want to get rid of cancelled tasks
6072
// even with the penalty of the clean up overhead
6173
scheduledThreadPool.setRemoveOnCancelPolicy(true);
62-
74+
scheduledThreadPool.setKeepAliveTime(THREAD_POOL_KEEP_ALIVE_TIME_IN_SECONDS, SECONDS);
75+
if (corePoolSize > 1) {
76+
scheduledThreadPool.allowCoreThreadTimeOut(true);
77+
}
6378
return scheduledThreadPool;
6479
}
6580

@@ -76,9 +91,7 @@ public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize,
7691
*/
7792
public static ScheduledExecutorService newSingleThreadScheduledExecutor(String name) {
7893
// reuse our custom method which configures removal of cancelled tasks
79-
ScheduledExecutorService singleThreadScheduledExecutor = newScheduledThreadPool(1, name);
80-
81-
return singleThreadScheduledExecutor;
94+
return newScheduledThreadPool(1, name);
8295
}
8396

8497
/**
@@ -94,7 +107,18 @@ public static ScheduledExecutorService newSingleThreadScheduledExecutor(String n
94107
* @return the newly created thread pool
95108
*/
96109
public static ExecutorService newFixedThreadPool(int nThreads, String name) {
97-
return Executors.newFixedThreadPool(nThreads, new ThreadFactoryWithNamePrefix(name));
110+
// @formatter:off
111+
ThreadPoolExecutor executor = new ThreadPoolExecutor(THREAD_POOL_CORE_THREADS_SIZE,
112+
nThreads,
113+
THREAD_POOL_KEEP_ALIVE_TIME_IN_SECONDS,
114+
SECONDS,
115+
new LinkedBlockingQueue<>(),
116+
new ThreadFactoryWithNamePrefix(name));
117+
// @formatter:on
118+
if (nThreads > 1) {
119+
executor.allowCoreThreadTimeOut(true);
120+
}
121+
return executor;
98122
}
99123

100124
/**

0 commit comments

Comments
 (0)