Skip to content

Commit

Permalink
Avoid race condition in executors
Browse files Browse the repository at this point in the history
  • Loading branch information
kmsbernard committed Apr 3, 2021
1 parent 50a4d98 commit f7f1c1d
Showing 1 changed file with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,15 @@ public ExecutorService getTransactionalExecutor(String identifier) {

public ExecutorService getExecutor(boolean isTransactional, String identifier) {
String executorName = getExecutorName(isTransactional, identifier);
ExecutorService existingExecutor = executors.get(executorName);
if (existingExecutor != null) return existingExecutor;
ExecutorService newExecutor = getNewExecutor(isTransactional);
executors.put(executorName, newExecutor);
return newExecutor;
synchronized(executors) {
ExecutorService existingExecutor = executors.get(executorName);
if (existingExecutor == null) {
ExecutorService newExecutor = getNewExecutor(isTransactional);
executors.put(executorName, newExecutor);
return newExecutor;
}
}
return existingExecutor;
}

private ExecutorService getNewExecutor(boolean isTransactional) {
Expand Down Expand Up @@ -106,10 +110,12 @@ public void shutdown() {
}

public void removeExecutor(String executorName) {
ExecutorService existingExecutor = executors.get(executorName);
if (existingExecutor != null) {
existingExecutor.shutdownNow();
executors.remove(executorName);
synchronized(executors) {
ExecutorService existingExecutor = executors.get(executorName);
if (existingExecutor != null) {
existingExecutor.shutdownNow();
executors.remove(executorName);
}
}
}
}

0 comments on commit f7f1c1d

Please sign in to comment.