Thread Safe Singleton Examples

 

The solution of Bill Pugh

public class Singleton {
        // Private constructor prevents instantiation from other classes
        private Singleton() { }
 
        /**
        * SingletonHolder is loaded on the first execution of Singleton.getInstance() 
        * or the first access to SingletonHolder.INSTANCE, not before.
        */
        private static class SingletonHolder { 
                public static final Singleton INSTANCE = new Singleton();
        }
 
        public static Singleton getInstance() {
                return SingletonHolder.INSTANCE;
        }
}

References

* http://en.wikipedia.org/wiki/Singleton_pattern

This entry was posted in java and tagged , , , , . Bookmark the permalink.

One Response to Thread Safe Singleton Examples

  1. BestOrville says:

    I see you don’t monetize jianmingli.com, don’t waste your traffic, you can earn extra cash every month with
    new monetization method. This is the best adsense alternative
    for any type of website (they approve all sites), for more
    details simply search in gooogle: murgrabia’s tools

Leave a Reply

Your email address will not be published. Required fields are marked *


*

This site uses Akismet to reduce spam. Learn how your comment data is processed.