Theoretically, questions come into mind concerning the governance surrounding instantiation and synchronization of these singleton classes, especially in a multi-threaded environment.
In this post, we'll go over some approaches posted in the thread and provide a best option.
- Static Initizialization
- Double Checked Locking (later only on post 1.5 jvm)
- Initialize-On-Demand Holder Class Idiom (MOST PREFERRED)
Static Initialization
class BeanBag { private static final SomeBean someBean = new SomeBean(); private static final AnotherBean anotherBean = new AnotherBean(); public static SomeBean getSomeBean() { return someBean; } public static AnotherBean getAnotherBean() { return anotherBean; } }
The initialization happens at the loading time of the class but there is no guarantee to be synchronized. The static class property someBean will be available at first call to getSomeBean(). This works 99% of the time, I have never had a problem with it.
Double Lock Checking
class BeanBag { private static volatile SomeBean someBean = null; public static SomeBean getSomeBean() { if (someBean==null){ synchronized(BeanBag.class){ if (someBean==null){ someBean = new SomeBean(); } } } return someBean; } }
The difference here, is the singleton instance is not actually instantiated until the getSomeBean() method is called. This provides a more lazy instantiation paradigm if needed. Here we can synchronize the block when SomeBean is instantiated.
Initialize-On-Demand Holder Class Idiom
class BeanBag { //Inner Class private static class BeanBagHolder { public static SomeBean someBean = new SomeBean(); } public static Something getInstance() { return BeanBagHolder.someBean; } }
This idiom derives its thread safety from the fact that operations that are part of class initialization, which is guaranteed by the JVM. It derives its lazy initialization from the fact that the inner class is not loaded until some thread references one of its fields or methods. This provides the best of both options and guaranteed to be thread-safe by the JVM.
The last option is the most preferred and this article goes into greater detail about Java Memory Model.
Most helpful packers and mvoers website for home shifting:
ReplyDeleteNoida packers and movers
Delhi packers and movers
Gurgaon packers and movers
Faridabad packers and movers
Ghaziabad packers and movers
Great and Useful Article.
ReplyDeleteOnline Java Training | Online Java Training
I have read your blog its very attractive and impressive. I like it your blog.
ReplyDeleteJava Training in Chennai Java Training in Chennai | Core Java Training in Chennai
Online Java Training Java Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai
Other budgetary organizations around the country that give this administration, and it isn't just limited to physical abiding stores, yet additionally e-Stores or Electronic Banking. check cashing san diego
ReplyDelete