{"id":2145,"date":"2017-04-05T05:05:50","date_gmt":"2017-04-05T05:05:50","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=2145"},"modified":"2017-04-05T05:12:45","modified_gmt":"2017-04-05T05:12:45","slug":"how-double-checked-locking-works-in-singleton","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/how-double-checked-locking-works-in-singleton\/","title":{"rendered":"How Double Checked Locking works in Singleton"},"content":{"rendered":"<p>Lets Consider the below Singleton code which uses Double Checked Locking<\/p>\n<p><strong>DoubleCheckLocking.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class DoubleCheckLocking \r\n{\r\n    public static class SearchBox \r\n    {\r\n        private static volatile SearchBox searchBox;\r\n\r\n        \/\/Private constructor\r\n        private SearchBox() {}\r\n\r\n        \/\/Static method to get instance\r\n        public static SearchBox getInstance() {\r\n            if (searchBox == null) { \/\/ first time lock\r\n                synchronized (SearchBox.class) {\r\n                    if (searchBox == null) {  \/\/ second time lock\r\n                        searchBox = new SearchBox();\r\n                    }\r\n                }\r\n            }\r\n            return searchBox;\r\n        }\r\n}\r\n<\/pre>\n<p>Lets dive deep into the code where Double Checking actually takes place<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n  if (searchBox == null) { \/\/ first time lock\r\n                synchronized (SearchBox.class) {\r\n                    if (searchBox == null) {  \/\/ second time lock\r\n                        searchBox = new SearchBox();\r\n                    }\r\n                }\r\n            }\r\n<\/pre>\n<ol>\n<li>Lets say we have two threads A and B and lets assume that atleast one of them reaches line 3 and observes searchBox == null is true. <\/li>\n<li>Two threads can not both be at line 3 at the same time because of the synchronized block. This is the key to understanding why double-checked locking works. <\/li>\n<li>So, it must the case that either A or B made it through synchronized first. Without loss of generality, say that that thread is A. Then, upon seeing searchBox == null is true, it will enter the body of the statement, and set searchBox to a new instance of SearchBox. It will then eventually exit the synchronized block. <\/li>\n<li>Now it will be  B&#8217;s turn to enter: remember, B was blocked waiting for A to exit. Now when it enters the block, it will observe searchBox. But A will have left just having set searchBox to a non-null value. <\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Lets Consider the below Singleton code which uses Double Checked Locking DoubleCheckLocking.java public class DoubleCheckLocking { public static class SearchBox { private static volatile SearchBox searchBox; \/\/Private constructor private SearchBox() {} \/\/Static method to get instance public static SearchBox getInstance() { if (searchBox == null) { \/\/ first time lock synchronized (SearchBox.class) { if (searchBox&hellip; <a href=\"https:\/\/codethataint.com\/blog\/how-double-checked-locking-works-in-singleton\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[227],"tags":[],"class_list":["post-2145","post","type-post","status-publish","format-standard","hentry","category-singleton"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=2145"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2145\/revisions"}],"predecessor-version":[{"id":2146,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2145\/revisions\/2146"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=2145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=2145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=2145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}