Why add synchronization keywords in the getOrInitExecutors method after using concurrent collection classes?
private static Map<String, ExecutorService> executors = new ConcurrentHashMap<>();
public static ExecutorService getOrInitExecutors(String poolName, int poolSize){
ExecutorService executor = executors.get(poolName);
if(null == executor){
synchronized (TaskProcessUtil.class){
executor = executors.get(poolName);
if(null == executor){
executor = init(poolName, poolSize);
executors.put(poolName, executor);
}
}
}
return executor;
}
I want to know the reason for writing this way
New contributor
user23532143 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.