Closed Macarse closed 9 years ago
@SeyelentEco: Whatever you guys prefer using.
In my app I call network-connection-class
and device-year-class
from different threads and I noticed more than one instance. I proposed the same change to device-network-class
(https://github.com/facebook/device-year-class/pull/2) and it was accepted.
I forked both libraries with this fix in and I don't see the problem any more.
It was an oversight in the device-year-class pull request (the variable not being volatile). We'll fix it there and implement the Initialization-on-demand holder idiom here.
@SeyelentEco go for it! If you release after fixing I would happily kill my forks :)
Fixed in https://github.com/facebook/network-connection-class/pull/3 . Will create new release soon.
Thanks for catching this, however I don't think double check locking is the right approach for this. We should go with the Initialization-on-demand holder idiom since it was developed specifically for lazy instantiation of singletons on Java.
Also, for future reference, the variable must be declared volatile or double check locking won't work correctly. See: http://en.wikipedia.org/wiki/Double-checked_locking