diff --git a/hystrix-core/src/main/java/com/netflix/hystrix/util/HystrixTimer.java b/hystrix-core/src/main/java/com/netflix/hystrix/util/HystrixTimer.java index 0c14b6e57..e07afaef9 100644 --- a/hystrix-core/src/main/java/com/netflix/hystrix/util/HystrixTimer.java +++ b/hystrix-core/src/main/java/com/netflix/hystrix/util/HystrixTimer.java @@ -134,7 +134,7 @@ public void clear() { */ protected void startThreadIfNeeded() { // create and start thread if one doesn't exist - if (executor.get() == null) { + while (executor.get() == null || ! executor.get().isInitialized()) { if (executor.compareAndSet(null, new ScheduledExecutor())) { // initialize the executor that we 'won' setting executor.get().initialize(); @@ -144,6 +144,7 @@ protected void startThreadIfNeeded() { private static class ScheduledExecutor { private volatile ScheduledThreadPoolExecutor executor; + private volatile boolean initialized; /** * We want this only done once when created in compareAndSet so use an initialize method @@ -158,11 +159,16 @@ public Thread newThread(Runnable r) { } }); + initialized = true; } public ScheduledThreadPoolExecutor getThreadPool() { return executor; } + + public boolean isInitialized() { + return initialized; + } } public static interface TimerListener { @@ -390,4 +396,4 @@ public int getIntervalTimeInMilliseconds() { } } -} \ No newline at end of file +}