From 329c67fa3634a667bde346590bb8916af618ad37 Mon Sep 17 00:00:00 2001 From: Pavel Baranchikov Date: Thu, 6 Oct 2016 19:33:29 +0400 Subject: [PATCH] Make InetUtils.executorService an instance variable. Fixes gh-132 --- .../cloud/commons/util/InetUtils.java | 43 ++++++------------- 1 file changed, 13 insertions(+), 30 deletions(-) diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/InetUtils.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/InetUtils.java index 12b4a174..7a6e3fd7 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/InetUtils.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/InetUtils.java @@ -25,44 +25,27 @@ import lombok.extern.apachecommons.CommonsLog; public class InetUtils implements Closeable { // TODO: maybe shutdown the thread pool if it isn't being used? - private static ExecutorService executorService; + private final ExecutorService executorService; private final InetUtilsProperties properties; private static final InetUtils instance = new InetUtils(new InetUtilsProperties()); public InetUtils(final InetUtilsProperties properties) { this.properties = properties; + this.executorService = Executors + .newSingleThreadExecutor(new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + Thread thread = new Thread(r); + thread.setName(InetUtilsProperties.PREFIX); + thread.setDaemon(true); + return thread; + } + }); } @Override public void close() { - if (executorService != null) { - synchronized (InetUtils.class) { - if (executorService != null) { - executorService.shutdown(); - executorService = null; - } - } - } - } - - private ExecutorService getExecutor() { - if (executorService == null) { - synchronized (InetUtils.class) { - if (executorService == null) { - executorService = Executors - .newSingleThreadExecutor(new ThreadFactory() { - @Override - public Thread newThread(Runnable r) { - Thread thread = new Thread(r); - thread.setName(InetUtilsProperties.PREFIX); - thread.setDaemon(true); - return thread; - } - }); - } - } - } - return executorService; + executorService.shutdown(); } public HostInfo findFirstNonLoopbackHostInfo() { @@ -156,7 +139,7 @@ public class InetUtils implements Closeable { public HostInfo convertAddress(final InetAddress address) { HostInfo hostInfo = new HostInfo(); - Future result = getExecutor().submit(new Callable() { + Future result = executorService.submit(new Callable() { @Override public String call() throws Exception { return address.getHostName();