From: Sébastien Lesaint Date: Thu, 23 Nov 2017 09:44:32 +0000 (+0100) Subject: SONAR-10104 use fixed ThreadPool with timeout on core threads X-Git-Tag: 7.0-RC1~289 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=08a8732ee8d235b1e28d499e35ac5b1bd37df3cb;p=sonarqube.git 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 --- 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