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

Remove double-check locking in CoapClient #1438

Merged
merged 1 commit into from
Oct 29, 2020
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 @@ -108,13 +108,13 @@ public class CoapClient {
private ExecutorService executor;

/** Scheduled executor intended to be used for rare executing timers (e.g. cleanup tasks). */
private ScheduledThreadPoolExecutor secondaryExecutor;
private volatile ScheduledThreadPoolExecutor secondaryExecutor;

/**
* Indicate, it the client-specific executor service is detached, or
* shutdown with this client.
*/
private boolean detachExecutor;
private volatile boolean detachExecutor;

/** The endpoint. */
private Endpoint endpoint;
Expand Down Expand Up @@ -327,16 +327,15 @@ public CoapClient setExecutors(ExecutorService executor, ScheduledThreadPoolExec
return this;
}

private ScheduledThreadPoolExecutor getSecondaryExecutor() {
private synchronized ScheduledThreadPoolExecutor getSecondaryExecutor() {
// Warning there is maybe a performance issue here, see :
// - https://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java
// - https://github.com/eclipse/californium/issues/1420
if (secondaryExecutor == null) {
synchronized (this) {
if (secondaryExecutor == null) {
secondaryExecutor = new ScheduledThreadPoolExecutor(1,
new NamedThreadFactory("CoapClient(secondary)#"));
}
this.detachExecutor = false;
}
secondaryExecutor = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory("CoapClient(secondary)#"));
}
this.detachExecutor = false;

return secondaryExecutor;
}

Expand Down