Skip to content

Commit

Permalink
Introduced Thread Pool Optimizations
Browse files Browse the repository at this point in the history
fixes #1392

Signed-off-by: Amit Kumar Mondal <[email protected]>
  • Loading branch information
amitjoy committed Apr 25, 2023
1 parent 51d6503 commit 7af87f3
Showing 1 changed file with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}

/**
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 7af87f3

Please sign in to comment.