This week's book giveaway is in the Agile forum.
We're giving away four copies of The Software Craftsman and have Sandro Mancuso on-line!
See this thread for details.
The moose likes Java in General and the fly likes Question regarding Normal Singleton Pattern vs. LazyLoad Singleton Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question regarding Normal Singleton Pattern vs. LazyLoad Singleton Pattern" Watch "Question regarding Normal Singleton Pattern vs. LazyLoad Singleton Pattern" New topic
Author

Question regarding Normal Singleton Pattern vs. LazyLoad Singleton Pattern

Rahul Shivsharan
Ranch Hand

Joined: Jun 17, 2009
Posts: 83



In the above code i made a singleton class SingletonObject01.java, which gives single instance of it in multithreading environment also

now if i use Lazyload singleton as below,



This also gives the only one instance of class SingletonObject02.java that too in multithreading enviroment

Now,
My question is in my both of above approach i am not using synchronized key work ,
So which approach is better for Singleton Pattern from above two



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41576
    
  31
Too difficult a question for "beginning". Moving thread.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 19357
    
  10

Which is better? I would say the first is better, because it's shorter and easier to understand.

In terms of execution time and resources and so on, both of them are trivial code which do essentially nothing and consume essentially no resources, so there is hardly any difference between the two.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question regarding Normal Singleton Pattern vs. LazyLoad Singleton Pattern