Re: [go-nuts] double checking is right for singleton

2018-12-06 Thread Chao Yuepan
got it. Thanks Ian 在 2018年12月7日星期五 UTC+8下午12:59:49,Ian Lance Taylor写道: > > On Thu, Dec 6, 2018 at 8:45 PM Chao Yuepan > wrote: > > > > Java implements singleton by double-checking must use keyword volatile : > > > > > > class Foo { > > private volatile Bar bar; > > > > public Bar getB

Re: [go-nuts] double checking is right for singleton

2018-12-06 Thread Ian Lance Taylor
On Thu, Dec 6, 2018 at 8:45 PM Chao Yuepan wrote: > > Java implements singleton by double-checking must use keyword volatile : > > > class Foo { > private volatile Bar bar; > > public Bar getBar() { > Bar value = bar; > if (value == null) { > synchronized (this) { > value

[go-nuts] double checking is right for singleton

2018-12-06 Thread Chao Yuepan
Java implements singleton by double-checking must use keyword volatile : class Foo { private volatile Bar bar; public Bar getBar() { Bar value = bar; if (value == null) { synchronized (this) { value = bar; if (value == null) { bar = value = computeBar(