Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Smart thread safe implementation of the singleton pattern  (Read 3042 times)
0 Members and 1 Guest are viewing this topic.
Offline gouessej

« In padded room »



TUER


« Posted 2012-01-16 12:09:21 »

Hi

I have just found this on Wikipedia:
http://en.wikipedia.org/wiki/Initialization_on_demand_holder_idiom
Quote
public class Something {
        private Something() {
        }
 
        private static class LazyHolder {
                public static final Something INSTANCE = new Something();
        }
 
        public static Something getInstance() {
                return LazyHolder.INSTANCE;
        }
}

In which cases should we use an atomic reference instead of the example above?

Online Roquen
« Reply #1 - Posted 2012-01-16 12:22:50 »

The idea is if creating "Something" takes awhile to create, then to not bother until it is first used.  I'm not sure why you're comparing this to an atomic reference (but maybe my brain's not working ATM), but it probably would make sense to create INSTANCE atomically in the given code example.
Offline gouessej

« In padded room »



TUER


« Reply #2 - Posted 2012-01-16 12:43:01 »

The idea is if creating "Something" takes awhile to create, then to not bother until it is first used.  I'm not sure why you're comparing this to an atomic reference (but maybe my brain's not working ATM), but it probably would make sense to create INSTANCE atomically in the given code example.
I don't understand why he suggested a much complicated approach relying on atomic references:
http://timezra.blogspot.com/2008/04/threadsafe-lazy-instantiation-without.html

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Roquen
« Reply #3 - Posted 2012-01-16 14:37:21 »

He's attempting to make it thread-safe without using a synchronized block.
Offline cylab

JGO Knight


Medals: 34



« Reply #4 - Posted 2012-01-16 16:06:20 »

He is after lazy initializing non-static fields in instances, not lazy initializing singletons. So basically over-engineered nonsense Wink

Mathias - I Know What [you] Did Last Summer!
Offline gouessej

« In padded room »



TUER


« Reply #5 - Posted 2012-01-16 16:31:50 »

He's attempting to make it thread-safe without using a synchronized block.
But the example of Wikipedia already does it  Huh

He is after lazy initializing non-static fields in instances, not lazy initializing singletons. So basically over-engineered nonsense Wink
Lol you reassure me. I felt stupid when I didn't succeed in understanding what for he did that.

Offline sproingie
« Reply #6 - Posted 2012-01-16 17:56:08 »

Just use a synchronized block.  Actually you need two (aka "double checked locking", and no it isn't broken anymore).  Better yet, stop inventing your own singleton pattern, which in that form is really nothing more than an antipattern, and use factory methods that enforce the "singleton-ness" in the factory and not in the class.
Offline gouessej

« In padded room »



TUER


« Reply #7 - Posted 2012-01-16 19:24:55 »

Just use a synchronized block.  Actually you need two (aka "double checked locking", and no it isn't broken anymore). 
Are you sure it isn't broken anymore?

Better yet, stop inventing your own singleton pattern, which in that form is really nothing more than an antipattern, and use factory methods that enforce the "singleton-ness" in the factory and not in the class.
Yes singleton is an antipattern but when I begin using a new API, I cannot immediately suggest design changes. I prefer using a factory in my own projects.

Offline sproingie
« Reply #8 - Posted 2012-01-16 19:51:39 »

Are you sure it isn't broken anymore?

As sure as I am that there aren't bugs in the implementation of the java memory model, yes, I'm quite sure.  The JMM was created to address issues like the double-checked locking bug.  The only thing "broken" about the idiom now is how slow it is, but that's the price paid for safety (and it's still faster than synchronizing on all accesses).
Offline cylab

JGO Knight


Medals: 34



« Reply #9 - Posted 2012-01-17 12:37:46 »

Are you sure it isn't broken anymore?

As sure as I am that there aren't bugs in the implementation of the java memory model, yes, I'm quite sure.  The JMM was created to address issues like the double-checked locking bug.  The only thing "broken" about the idiom now is how slow it is, but that's the price paid for safety (and it's still faster than synchronizing on all accesses).


You mean double-checked locking using volatile booleans!?

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie
« Reply #10 - Posted 2012-01-17 17:42:25 »

You mean double-checked locking using volatile booleans!?

No, not volatile booleans.  One lock, two checks against null.  Two locks if you count a synchronized setter, but volatile does the job for this idiom.  

http://en.wikipedia.org/wiki/Double-checked_locking
http://en.wikipedia.org/wiki/Initialization_on_demand_holder_idiom

The second link is the pattern in the original post, but read all the way to "when not to use it" for one reason I don't recommend it as a general pattern.  The second is that I'm just not big on the GoF Singleton implementation in general.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (23 views)
2014-04-15 18:08:23

BurntPizza (18 views)
2014-04-15 03:46:01

UprightPath (32 views)
2014-04-14 17:39:50

UprightPath (16 views)
2014-04-14 17:35:47

Porlus (32 views)
2014-04-14 15:48:38

tom_mai78101 (58 views)
2014-04-10 04:04:31

BurntPizza (116 views)
2014-04-08 23:06:04

tom_mai78101 (216 views)
2014-04-05 13:34:39

trollwarrior1 (183 views)
2014-04-04 12:06:45

CJLetsGame (190 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!