{"id":22,"date":"2007-12-22T14:09:50","date_gmt":"2007-12-22T21:09:50","guid":{"rendered":"http:\/\/shivdev.com\/blog\/2008\/05\/22\/thread-safe-singleton\/"},"modified":"2011-05-06T07:33:55","modified_gmt":"2011-05-06T07:33:55","slug":"thread-safe-singleton","status":"publish","type":"post","link":"http:\/\/shivdev.com\/blog\/2007\/12\/22\/thread-safe-singleton\/","title":{"rendered":"Thread Safe Singleton"},"content":{"rendered":"<p>Here is an example of a <strong>Simple Singleton<\/strong>. There are 3 things to remember.<\/p>\n<ul>\n<li>Create a private static instance of the singleton class.<\/li>\n<li>The constructor must be private.<\/li>\n<li>There must be a static method to get the &#8220;single&#8221; instance of the singleton class.<\/li>\n<\/ul>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/* This is NOT Thread Safe - This is a Simple Example of a Singleton*\/\r\npublic class SimpleSingleton {\r\n    private static SimpleSingleton instance = null;    \r\n    private SimpleSingleton () {\r\n    }\r\n\r\n    public static SimpleSingleton getInstance() {\r\n        \/\/ lazily create an instance\r\n        if (instance == null) {\r\n            instance = new SimpleSingleton ();\r\n        }\r\n        return instance;\r\n    }\r\n}<\/pre>\n<p>Now here is an example of a <strong>Thread Safe Singleton<\/strong> achieved using the <em>synchronized(instance)<\/em>. The reason for checking <em>(instance == null)<\/em> twice is so that we don&#8217;t want all threads to wait when getInstance() is called. Threads must wait only when instance is being created.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class ThreadSafeSingleton {\r\n    private static ThreadSafeSingleton instance = null;    \r\n\r\n    private ThreadSafeSingleton() {\r\n    }\r\n\r\n    public static ThreadSafeSingleton getInstance() {\r\n        \/\/ Threads will acquire a lock and wait ONLY if instance is NOT NULL\r\n        if (instance == null) {\r\n                \/\/ Only One Thread allowed at a time from this point\r\n                synchronized (instance) {\r\n                        if (instance == null) {\r\n                            instance = new ThreadSafeSingleton();\r\n                        }\r\n                } \/\/ End of Synchronized Block\r\n        }\r\n        return instance;\r\n    }\r\n}<\/pre>\n<p>Another Thread Safe Singleton Example<br \/>\nAnother way of achieving a <strong>Thread Safe Singleton<\/strong> without worrying about synchronization, is by instantiating the Singleton object when the CLASS gets loaded (in the <strong>static <\/strong>block). However, the we lose the &#8220;lazy initialization&#8221; nature of the ThreadSafeSingleton shown above.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Singleton {\r\n    static {\r\n        \/\/ This happens when the class is loaded.\r\n        instance = new Singleton();\r\n    }\r\n\r\n    private static Singleton instance = null;    \r\n\r\n    private Singleton () {\r\n    }\r\n\r\n    public static Singleton getInstance() {\r\n        return instance;\r\n    }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is an example of a Simple Singleton. There are 3 things to remember. Create a private static instance of the singleton class. The constructor must be private. There must be a static method to get the &#8220;single&#8221; instance of the singleton class. Now here is an example of a Thread Safe Singleton achieved using [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10,11],"tags":[],"_links":{"self":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts\/22"}],"collection":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/comments?post=22"}],"version-history":[{"count":1,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts\/22\/revisions"}],"predecessor-version":[{"id":193,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts\/22\/revisions\/193"}],"wp:attachment":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/categories?post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/tags?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}