From 08a8732ee8d235b1e28d499e35ac5b1bd37df3cb Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 23 Nov 2017 10:44:32 +0100 Subject: [PATCH] SONAR-10104 use fixed ThreadPool with timeout on core threads this strategy really allows to use multiple threads when queue is growing and little to no workers when its small --- .../AsyncExecutionExecutorServiceImpl.java | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/async/AsyncExecutionExecutorServiceImpl.java b/server/sonar-server/src/main/java/org/sonar/server/async/AsyncExecutionExecutorServiceImpl.java index f329d52ec40..fe3d4f83088 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/async/AsyncExecutionExecutorServiceImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/async/AsyncExecutionExecutorServiceImpl.java @@ -26,29 +26,33 @@ import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.server.util.AbstractStoppableExecutorService; -import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.MINUTES; public class AsyncExecutionExecutorServiceImpl extends AbstractStoppableExecutorService implements AsyncExecutionExecutorService, AsyncExecutionMonitoring { private static final Logger LOG = Loggers.get(AsyncExecutionExecutorServiceImpl.class); - private static final int MIN_THREAD_COUNT = 1; private static final int MAX_THREAD_COUNT = 10; - private static final int MAX_QUEUE_SIZE = Integer.MAX_VALUE; - private static final long KEEP_ALIVE_TIME_IN_MILLISECONDS = 0L; + private static final int UNLIMITED_QUEUE = Integer.MAX_VALUE; + private static final long KEEP_ALIVE_TIME_IN_MINUTES = 5L; public AsyncExecutionExecutorServiceImpl() { - super( - new ThreadPoolExecutor( - MIN_THREAD_COUNT, MAX_THREAD_COUNT, - KEEP_ALIVE_TIME_IN_MILLISECONDS, MILLISECONDS, - new LinkedBlockingQueue<>(MAX_QUEUE_SIZE), - new ThreadFactoryBuilder() - .setDaemon(false) - .setNameFormat("SQ_async-%d") - .setUncaughtExceptionHandler(((t, e) -> LOG.error("Thread " + t + " failed unexpectedly", e))) - .build())); + super(createDelegate()); + } + + private static ThreadPoolExecutor createDelegate() { + ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor( + MAX_THREAD_COUNT, MAX_THREAD_COUNT, + KEEP_ALIVE_TIME_IN_MINUTES, MINUTES, + new LinkedBlockingQueue<>(UNLIMITED_QUEUE), + new ThreadFactoryBuilder() + .setDaemon(false) + .setNameFormat("SQ_async-%d") + .setUncaughtExceptionHandler(((t, e) -> LOG.error("Thread " + t + " failed unexpectedly", e))) + .build()); + threadPoolExecutor.allowCoreThreadTimeOut(true); + return threadPoolExecutor; } @Override -- 2.39.5