Skip to content

Commit

Permalink
ThreadPoolExecutor now uses LinkedBlockingQueue instead of Synchronou…
Browse files Browse the repository at this point in the history
…sQueue. See comments in the code.

Signed-off-by: Ceki Gulcu <[email protected]>
  • Loading branch information
ceki committed Dec 2, 2024
1 parent 69ce513 commit 433e168
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
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.TimeUnit;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

import ch.qos.logback.core.CoreConstants;
Expand Down Expand Up @@ -60,8 +53,7 @@ public Thread newThread(Runnable r) {
};

static public ScheduledExecutorService newScheduledExecutorService() {
return new ScheduledThreadPoolExecutor(CoreConstants.SCHEDULED_EXECUTOR_POOL_SIZE,
THREAD_FACTORY_FOR_SCHEDULED_EXECUTION_SERVICE);
return new ScheduledThreadPoolExecutor(CoreConstants.SCHEDULED_EXECUTOR_POOL_SIZE, THREAD_FACTORY_FOR_SCHEDULED_EXECUTION_SERVICE);
}

/**
Expand All @@ -78,9 +70,20 @@ static public ExecutorService newExecutorService() {
* @return ThreadPoolExecutor
*/
static public ThreadPoolExecutor newThreadPoolExecutor() {
return new ThreadPoolExecutor(CoreConstants.CORE_POOL_SIZE, CoreConstants.MAX_POOL_SIZE, 0L,
TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(),
THREAD_FACTORY_FOR_SCHEDULED_EXECUTION_SERVICE);

// irrelevant parameter when LinkedBlockingQueue is in use
final int maximumPoolSize = CoreConstants.CORE_POOL_SIZE + 1;
final long keepAliveMillis = 100L;

// As of version 1.5.13, the SynchronousQueue was replaced by LinkedBlockingQueue
// This has the effect of queueing jobs immediately and have them run by CORE_POOL_SIZE
// threads. We expect jobs to arrive at a relatively slow pace compared to their duration.
// Note that threads are removed if idle more than keepAliveMillis
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(CoreConstants.CORE_POOL_SIZE, maximumPoolSize, keepAliveMillis, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(), THREAD_FACTORY_FOR_SCHEDULED_EXECUTION_SERVICE);
threadPoolExecutor.allowCoreThreadTimeOut(true);
return threadPoolExecutor;

}

/**
Expand Down

This file was deleted.

0 comments on commit 433e168

Please sign in to comment.