[jira] [Commented] (NETBEANS-5136) double checked locking idiom wrong in inspect and transform
[ https://issues.apache.org/jira/browse/NETBEANS-5136?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17280303#comment-17280303 ] Carsten Hammer commented on NETBEANS-5136: -- Hi Jan, the code that works according to the github bug description looks like this: {code:java} private volatile String init; public String get() { String init= this.init; if (init == null) { // First check (no locking) synchronized (this) { if (this.init == null) // Second check (with locking) this.init = init= computeFieldValue(); else init= this.init; } } return init; } {code} So the difference is {code:java} init = this.init; if (init == null) { this.init = init = ""; } {code} vs {code:java} if (this.init == null) // Second check (with locking) this.init = init= computeFieldValue(); else init= this.init; {code} within the synchronized section. Is this the same? It doesn't look like it is the same. But maybe similar enough and in the end the netbeans variant is identical to the older code from Joshua Bloch published at https://www.oracle.com/technical-resources/articles/javase/bloch-effective-08-qa.html that seems to be fine too. So - apologies - your code seems to be ok! I close this now. As a sidenote I still prefer the variant in the description of this issue but of course this is a matter of personal preference. > double checked locking idiom wrong in inspect and transform > --- > > Key: NETBEANS-5136 > URL: https://issues.apache.org/jira/browse/NETBEANS-5136 > Project: NetBeans > Issue Type: Bug > Components: java - Refactoring >Affects Versions: 12.2 >Reporter: Carsten Hammer >Priority: Critical > > The pattern implemented in > [https://bz.apache.org/netbeans/show_bug.cgi?id=248740] seems to be wrong > according to > [https://docs.google.com/document/d/1mAeEgQu4H4ADxa03k7YaVDjIP5vJBvjVIjg3DIvoc8E/edit] > The right pattern looks like this: > # 334, Second code example. *_This is a serious error!_* The current code > can return null if multiple threads race to initialize the field. Here’s how > the code should look: > > {code:java} > // Double-check idiom for lazy initialization of instance fields > private volatile FieldType field; > > private FieldType getField() { > FieldType result = field; > if (result != null) // First check (no locking) > return result; > > synchronized(this){ > if (field == null) // Second check (with locking) > field = computeFieldValue(); > return field; > } > } > {code} -- This message was sent by Atlassian Jira (v8.3.4#803005) - To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org For additional commands, e-mail: commits-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
[jira] [Commented] (NETBEANS-5136) double checked locking idiom wrong in inspect and transform
[ https://issues.apache.org/jira/browse/NETBEANS-5136?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17280288#comment-17280288 ] Jan Lahoda commented on NETBEANS-5136: -- So, starting with this obviously broken code: {code:java} private String init; public String get() { if (init == null) { synchronized (this) { if (init == null) { init = ""; } } } return init; } {code} NetBeans will (among others) offer to change it to: {code:java} private volatile String init; public String get() { String init = this.init; if (init == null) { synchronized (this) { init = this.init; if (init == null) { this.init = init = ""; } } } return init; } {code} Sorry, but this is not the same code as shown in the bug you quote. In the bug you quote, the local variable ("result" there) can be left null after the synchronized block is finished. But this is not the case here: the local variable ("init" here) is always assigned inside the synchronized block. So, not clear what is the error here. Please be more precise and describe exactly what is wrong. Thanks. > double checked locking idiom wrong in inspect and transform > --- > > Key: NETBEANS-5136 > URL: https://issues.apache.org/jira/browse/NETBEANS-5136 > Project: NetBeans > Issue Type: Bug > Components: java - Refactoring >Affects Versions: 12.2 >Reporter: Carsten Hammer >Priority: Critical > > The pattern implemented in > [https://bz.apache.org/netbeans/show_bug.cgi?id=248740] seems to be wrong > according to > [https://docs.google.com/document/d/1mAeEgQu4H4ADxa03k7YaVDjIP5vJBvjVIjg3DIvoc8E/edit] > The right pattern looks like this: > # 334, Second code example. *_This is a serious error!_* The current code > can return null if multiple threads race to initialize the field. Here’s how > the code should look: > > {code:java} > // Double-check idiom for lazy initialization of instance fields > private volatile FieldType field; > > private FieldType getField() { > FieldType result = field; > if (result != null) // First check (no locking) > return result; > > synchronized(this){ > if (field == null) // Second check (with locking) > field = computeFieldValue(); > return field; > } > } > {code} -- This message was sent by Atlassian Jira (v8.3.4#803005) - To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org For additional commands, e-mail: commits-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
[jira] [Commented] (NETBEANS-5136) double checked locking idiom wrong in inspect and transform
[ https://issues.apache.org/jira/browse/NETBEANS-5136?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17280278#comment-17280278 ] Carsten Hammer commented on NETBEANS-5136: -- See explanation at [https://github.com/jbloch/effective-java-3e-source-code/issues/8] > double checked locking idiom wrong in inspect and transform > --- > > Key: NETBEANS-5136 > URL: https://issues.apache.org/jira/browse/NETBEANS-5136 > Project: NetBeans > Issue Type: Bug > Components: java - Refactoring >Affects Versions: 12.2 >Reporter: Carsten Hammer >Priority: Critical > > The pattern implemented in > [https://bz.apache.org/netbeans/show_bug.cgi?id=248740] seems to be wrong > according to > [https://docs.google.com/document/d/1mAeEgQu4H4ADxa03k7YaVDjIP5vJBvjVIjg3DIvoc8E/edit] > The right pattern looks like this: > # 334, Second code example. *_This is a serious error!_* The current code > can return null if multiple threads race to initialize the field. Here’s how > the code should look: > > {code:java} > // Double-check idiom for lazy initialization of instance fields > private volatile FieldType field; > > private FieldType getField() { > FieldType result = field; > if (result != null) // First check (no locking) > return result; > > synchronized(this){ > if (field == null) // Second check (with locking) > field = computeFieldValue(); > return field; > } > } > {code} -- This message was sent by Atlassian Jira (v8.3.4#803005) - To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org For additional commands, e-mail: commits-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
[jira] [Commented] (NETBEANS-5136) double checked locking idiom wrong in inspect and transform
[ https://issues.apache.org/jira/browse/NETBEANS-5136?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17280275#comment-17280275 ] Jakub Herkel commented on NETBEANS-5136: I don't see anything wrong with code from [https://bz.apache.org/netbeans/show_bug.cgi?id=248740]. Could you describe what the problem is? > double checked locking idiom wrong in inspect and transform > --- > > Key: NETBEANS-5136 > URL: https://issues.apache.org/jira/browse/NETBEANS-5136 > Project: NetBeans > Issue Type: Bug > Components: java - Refactoring >Affects Versions: 12.2 >Reporter: Carsten Hammer >Priority: Critical > > The pattern implemented in > [https://bz.apache.org/netbeans/show_bug.cgi?id=248740] seems to be wrong > according to > [https://docs.google.com/document/d/1mAeEgQu4H4ADxa03k7YaVDjIP5vJBvjVIjg3DIvoc8E/edit] > The right pattern looks like this: > # 334, Second code example. *_This is a serious error!_* The current code > can return null if multiple threads race to initialize the field. Here’s how > the code should look: > > {code:java} > // Double-check idiom for lazy initialization of instance fields > private volatile FieldType field; > > private FieldType getField() { > FieldType result = field; > if (result != null) // First check (no locking) > return result; > > synchronized(this){ > if (field == null) // Second check (with locking) > field = computeFieldValue(); > return field; > } > } > {code} -- This message was sent by Atlassian Jira (v8.3.4#803005) - To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org For additional commands, e-mail: commits-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists