Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
1) Use a static holder class:
The SingletonHolder class is only loaded and initialized when first needed, which is on the first call to the getInstance() method. Also, class loading is atomic by itself.
2) Use an enum:
This way the singleton is also automatically safe for serialization.
Rob Prime wrote:1) Use a static holder class:
The SingletonHolder class is only loaded and initialized when first needed, which is on the first call to the getInstance() method. Also, class loading is atomic by itself.
2) Use an enum:
This way the singleton is also automatically safe for serialization.
If the enum singleton contains methods that modify shared member variables, would those methods need to be synchronize?
Sam Yim wrote:
If the enum singleton contains methods that modify shared member variables, would those methods need to be synchronize?
Yes, no matter what structures you use, if the object is used in multiple threads and the internal data is modified (in ways that will affect other threads' execution) then the code modifying and using the data should be synchronized. Nothing in how an enum works modifies that.
Steve Luke wrote:
Yes, no matter what structures you use, if the object is used in multiple threads and the internal data is modified (in ways that will affect other threads' execution) then the code modifying and using the data should be synchronized. Nothing in how an enum works modifies that.
That was my reasoning but wasn't sure. Thanks for the confirm.
I do some of my very best work in water. Like this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth