Skip to content

Commit

Permalink
Merge pull request #203 from rocketraman/init-thread-safety
Browse files Browse the repository at this point in the history
Made HystrixTimer initialization thread-safe
  • Loading branch information
benjchristensen committed Feb 7, 2014
2 parents d985938 + 45e0041 commit 6435c7c
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -390,4 +396,4 @@ public int getIntervalTimeInMilliseconds() {

}
}
}
}

0 comments on commit 6435c7c

Please sign in to comment.