I want to use a common thread pool that can be used throughout my application wherever I want. Shall I create a static Executor service in the main class. Then use it wherever needed? Currently I have this in my main class(MyMainApplication.java)
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
public static final ThreadPoolExecutor getExecutor(int corePoolSize, int maxPoolSize, int keepAlive) {
return ThreadPools.getExecutor(corePoolSize, maxPoolSize, keepAlive);
}
My threadpool class:
@Component
public class ThreadPools {
private static final int DEFAULT_CORE_POOL_SIZE = 5;
private static final int DEFAULT_MAX_POOL_SIZE = 10;
private static final int DEFAULT_KEEP_ALIVE_MS = 240;
private static int corePoolSize = DEFAULT_CORE_POOL_SIZE;
private static int maxPoolSize = DEFAULT_MAX_POOL_SIZE;
private static int poolKeepAliveInMillis = DEFAULT_KEEP_ALIVE_MS;
public static ThreadPoolExecutor getExecutor(int cpSize, int maxSize, int msTime) {
if (cpSize != 0) {
setCorePoolSize(cpSize);
}
if (maxSize != 0) {
setMaxPoolSize(maxSize);
}
if (msTime != 0) {
setKeepAlive(msTime);
}
return new ThreadPoolExecutor(corePoolSize, maxPoolSize, poolKeepAliveInMillis, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<Runnable>(corePoolSize));
}
public static void setCorePoolSize(int size) {
ThreadPools.corePoolSize = size;
}
public static void setMaxPoolSize(int size) {
ThreadPools.maxPoolSize = size;
}
public static void setKeepAlive(int time) {
ThreadPools.poolKeepAliveInMillis = time;
}
}
And in my implementation class(GetDetails.java), I am getting the executor the following way.
public void getDetails()
{
int corePoolSize=25;
int maxPoolSize=50;
int KeepAliveTimeMs=1000;
ExecutorService executor = MyMainApplication.getExecutor(corePoolSize,
maxPoolSize, keepAlive);
..........
..........
executor.execute(runnableTask);
}
My concern is for each call to the getDetails(), will it create a new executor service with a new set of pool. so for example in production environment. if there are about 100 requests for getDetails() , will it result in creating 100 executor service with each having a their own set of the threadpool which is 100 * (25 corePoolSize, 50 maxPoolSize, 1000 keepTimeAlive). or will all the request use a common executor service with a common/same threadpool which is (25 corePoolSize, 50 maxPoolSize, 1000 keepTimeAlive). To achieve this I have made the getExecutor() in main as static. Am I doing it correctly?