diff --git a/org.eclipse.xtext.ide/src/org/eclipse/xtext/ide/ExecutorServiceProvider.java b/org.eclipse.xtext.ide/src/org/eclipse/xtext/ide/ExecutorServiceProvider.java index 6175cfd76b..fcca8588cc 100644 --- a/org.eclipse.xtext.ide/src/org/eclipse/xtext/ide/ExecutorServiceProvider.java +++ b/org.eclipse.xtext.ide/src/org/eclipse/xtext/ide/ExecutorServiceProvider.java @@ -8,6 +8,8 @@ */ package org.eclipse.xtext.ide; +import java.util.Collections; +import java.util.HashMap; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -15,7 +17,6 @@ import org.eclipse.xtext.util.DisposableRegistry; import org.eclipse.xtext.util.IDisposable; -import com.google.common.collect.Maps; import com.google.inject.Inject; import com.google.inject.Provider; import com.google.inject.Singleton; @@ -25,33 +26,26 @@ *

* In some situations it is necessary to use multiple instances of executor services in order to avoid deadlocks. That * can be achieved with the {@link #get(String)}�method, which will return a different instance for each key. + *

*/ @Singleton public class ExecutorServiceProvider implements Provider, IDisposable { + + private final Map instanceCache = Collections + .synchronizedMap(new HashMap()); + @Inject public void registerTo(DisposableRegistry disposableRegistry) { disposableRegistry.register(this); } - private final Map instanceCache = Maps.newHashMapWithExpectedSize(3); - @Override public ExecutorService get() { return get(null); } public ExecutorService get(String key) { - ExecutorService result = instanceCache.get(key); - if (result == null) { - synchronized (instanceCache) { - result = instanceCache.get(key); - if (result == null) { - result = createInstance(key); - instanceCache.put(key, result); - } - } - } - return result; + return instanceCache.computeIfAbsent(key, this::createInstance); } protected ExecutorService createInstance(String key) { @@ -60,9 +54,11 @@ protected ExecutorService createInstance(String key) { @Override public void dispose() { - for (ExecutorService executorService : instanceCache.values()) { - executorService.shutdown(); + synchronized (instanceCache) { + for (ExecutorService executorService : instanceCache.values()) { + executorService.shutdown(); + } + instanceCache.clear(); } - instanceCache.clear(); } }