Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made HystrixTimer initialization thread-safe #203

Merged
merged 1 commit into from
Feb 7, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {

}
}
}
}