Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Smart thread safe implementation of the singleton pattern  (Read 949 times)
0 Members and 2 Guests are viewing this topic.
Offline gouessej

JGO Kernel
*****

Posts: 3560
Medals: 30


TUER


« on: 2012-01-16 06: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?

Julien Gouesse
Online Roquen

JGO Strike Force
***

Posts: 827
Medals: 25



« Reply #1 on: 2012-01-16 06: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

JGO Kernel
*****

Posts: 3560
Medals: 30


TUER


« Reply #2 on: 2012-01-16 06: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

Julien Gouesse
Games published by our own members! Go get 'em!
Online Roquen

JGO Strike Force
***

Posts: 827
Medals: 25



« Reply #3 on: 2012-01-16 08:37:21 »

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

JGO Kernel
*****

Posts: 1940
Medals: 27



« Reply #4 on: 2012-01-16 10: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

JGO Kernel
*****

Posts: 3560
Medals: 30


TUER


« Reply #5 on: 2012-01-16 10: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.

Julien Gouesse
Offline sproingie

JGO Strike Force
***

Posts: 899
Medals: 55



« Reply #6 on: 2012-01-16 11: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

JGO Kernel
*****

Posts: 3560
Medals: 30


TUER


« Reply #7 on: 2012-01-16 13: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.

Julien Gouesse
Offline sproingie

JGO Strike Force
***

Posts: 899
Medals: 55



« Reply #8 on: 2012-01-16 13: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 Kernel
*****

Posts: 1940
Medals: 27



« Reply #9 on: 2012-01-17 06: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! Go get 'em!
Offline sproingie

JGO Strike Force
***

Posts: 899
Medals: 55



« Reply #10 on: 2012-01-17 11: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]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.081 seconds with 19 queries.