-
Notifications
You must be signed in to change notification settings - Fork 369
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
Double-Checked Locking #1420
Comments
Is it OK, if I remove the "double check"? Or do you want to replace that with a more sophisticated solution? |
This is not clear to me at all. But reading this : https://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java Correct ways seems to be :
As 1. seems to be the recommended way since java 5, so would go for 1. |
AFAIK, there have been some discussions over the years, if that pattern should be applied or not. For modern java-vms, it seems to have not too much advantages. |
I'm curious any Source ?
I don't know, maybe you're right.
Personally, I would just follow the wikipedia page which considers the volatile stuff as the best practice for lazy loading giving explanation and source, but I have no strong opinion.
|
|
Interesting. I try to go deeper in this topic, not to decide what to do for this particular issue but more for my general knowledge.
I look at see references of this rule and I see nothing about this, neither what could be early version of the JVM. I try to find some information about this on the web and for now didn't find so much about that. Probably not directly linked but I fall on this interesting "news" : https://openjdk.java.net/jeps/374. (If one day you find more information about that do not hesitate to share) |
I would like to have it fixed for the 2.5.0. So, @sbernard31,would it be possible for you to provide a fix? |
No objection.
It seems that the current way I implemented double-check is not correct. /** Scheduled executor intended to be used for rare executing timers (e.g. cleanup tasks). */
private volatile ScheduledThreadPoolExecutor secondaryExecutor;
private volatile boolean detachExecutor;
private ScheduledThreadPoolExecutor getSecondaryExecutor() {
// This code comes from :
// https://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java
// https://www.oracle.com/technical-resources/articles/javase/bloch-effective-08-qa.html
// AND SHOULD NOT BE MODIFIED
ScheduledThreadPoolExecutor localRef = secondaryExecutor;
if (localRef == null) {
synchronized (this) {
localRef = secondaryExecutor;
if (localRef == null) {
secondaryExecutor = localRef = new ScheduledThreadPoolExecutor(1,
new NamedThreadFactory("CoapClient(secondary)#"));
}
this.detachExecutor = false;
}
}
return secondaryExecutor;
} Note :
As I didn't find any information about that. Alternatively, we can just remove the double check and keep in mind that we have maybe a performance issue here : /** Scheduled executor intended to be used for rare executing timers (e.g. cleanup tasks). */
private volatile ScheduledThreadPoolExecutor secondaryExecutor;
private volatile boolean detachExecutor;
private 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
synchronized (this) {
if (secondaryExecutor == null) {
secondaryExecutor = new ScheduledThreadPoolExecutor(1,
new NamedThreadFactory("CoapClient(secondary)#"));
this.detachExecutor = false;
}
}
} I have no strong opinion, but maybe second solution is safer ? |
I would prefer the second. |
Done. (see #1438) |
https://github.com/eclipse/californium/blob/688853649bcee162bd7925a6561a238c85bec1d6/californium-core/src/main/java/org/eclipse/californium/core/CoapClient.java#L331-L334
Double-Checked Locking is widely cited and used as an efficient method for implementing lazy initialization in a multithreaded environment.
Unfortunately, it will not work reliably in a platform independent way when implemented in Java, without additional synchronization.
The text was updated successfully, but these errors were encountered: