Introduction
Yesterday I was talking to a good friend in the park and the topic of double checked locking for lazy instantiating of singletons came up. (Why we talked about this on a nice sunny day in the park is a separate issue, which won't be addressed in the blog ;)) At the time I was sure it was broken but I couldn't explain the reasons behind this strange behaviour. So I decided to have a look at it and write a quick blog about it.
How does double check locking look like in Java:
public class DoubleCheckedLocking { private static DoubleCheckedLocking singleton; private final String a; private final String b; private DoubleCheckedLocking() { this.a = "A"; this.b = "B"; } public DoubleCheckedLocking getSingleton() { if(singleton == null) { //1 synchronized(this) { //2 if(singleton == null) { //3 singleton = new DoubleCheckedLocking(); //4 } } } return singleton; //5 } }The idea behind this pattern is that you want to lazy initialise your singleton but you want to avoid the performance penalty which comes from declaring the whole method
synchronized
. In the pattern the first thread would enter the method in line 1 and check that the singleton is null
. It would then synchronize on the objects internal lock on line 2 and do a null
check on the value of the singleton again on line 3. If the null check holds true the singleton will be initialised on line 5 finally. All further calls to the getSingleton method wouldn't see the singleton variable as null and would just return the cached instance.
Unfortunately this pattern is not thread-safe and is considered broken. What could possibly go wrong in this situation? The problem sits on line 4. Assigning a reference to a variable and running the constructor is not an atomic operation. Two things need to happen:
- Run the constructor
- Assign newly created instance to variable (singleton)
What can you do?
The easiest way to fix this is to not use lazy instantiation at all or synchronize the whole method.
What if you must have the use lazy loading and can't synchronize the whole method? You can also declare the variable
public class ThreadSafeLoading { private static final ThreadSafeLoading singleton = new ThreadSafeLoading(); public ThreadSafeLoading getSingleton() { return singleton; } }This works very well. The code to initiate a static variable runs during classloading time and before the class can be instantiated. The variable is declared final so it is ensured that all clients calling
getSingleton
will refer to the same instance of the class.
What if you must have the use lazy loading and can't synchronize the whole method? You can also declare the variable
singleton
as volatile. That way the JVM will bring memory barriers in place which will stop the possibility for out-of-order processing. Each write to volatile variable and each read from a volatile variable will create memory barrier and ensure that two threads will see actions in the other thread in proper order.public class DoubleCheckedLocking {
private static volatile DoubleCheckedLocking singleton;
private final String a;
private final String b;
private DoubleCheckedLocking() {
this.a = "A";
this.b = "B";
}
public DoubleCheckedLocking getSingleton() {
if(singleton == null) { //1
synchronized(this) { //2
if(singleton == null) { //3
singleton = new DoubleCheckedLocking(); //4
}
}
}
return singleton; //5
}
}
Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training India . Nowadays Java has tons of job opportunities on various vertical industry.
ReplyDeleteHenrik Eichenhardt'S Blog: Why Double Checked Locking Is Broken? >>>>> Download Now
Delete>>>>> Download Full
Henrik Eichenhardt'S Blog: Why Double Checked Locking Is Broken? >>>>> Download LINK
>>>>> Download Now
Henrik Eichenhardt'S Blog: Why Double Checked Locking Is Broken? >>>>> Download Full
>>>>> Download LINK Cu
Very informative blog! I liked it and was very helpful for me. Thanks for sharing. Do share more ideas regularly.
ReplyDeleteSpoken English Classes in Chennai
Best Spoken English Classes in Chennai
IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
English Speaking Classes in Mumbai
English Speaking Course in Mumbai
IELTS Classes in Mumbai
IELTS Coaching in Mumbai
IELTS Coaching in Anna Nagar
Spoken English Class in Anna Nagar
Excellent Post
ReplyDeleteYaaron Studios is one of the rapidly growing editing studios in Hyderabad. We are the best Video Editing services in Hyderabad. We provides best graphic works like logo reveals, corporate presentation Etc. And also we gives the best Outdoor/Indoor shoots and Ad Making services.
video editors studio in hyderabad
short film editors in hyderabad
corporate video editing studio in hyderabad
ad making company in hyderabad
Thanks for sharing
ReplyDelete"Pressure Vessel Design Course is one of the courses offered by Sanjary Academy in Hyderabad. We have offer professional
Engineering Course like Piping Design Course,QA / QC Course,document Controller course,pressure Vessel Design Course,
Welding Inspector Course, Quality Management Course, #Safety officer course."
Piping Design Course in India
Piping Design Course in Hyderabad
Piping Design Course in Hyderabad
QA / QC Course
QA / QC Course in india
QA / QC Course in Hyderabad
Document Controller course
Pressure Vessel Design Course
Welding Inspector Course
Quality Management Course
Quality Management Course in india
Safety officer course
Nice Blog... Thanks for sharing... by Ece Projects in Chennai
ReplyDeleteThanks for Sharing this useful blog...
ReplyDeletematlab project centers in chennai
Good!!! by IOT Training in Chennai
ReplyDeleteNice Blog!!! Thanks for Sharing this Blog!!!
ReplyDeleteCotton Sarees in Surat
Wholesale Designer Sarees manufactures in surat
Thank you for excellent article.You made an article that is interesting.
ReplyDeleteInformatica online job support from India|Informatica project support from India ,AWS online job support from India|AWS project support from India|ETL Testing online job support from India|ETL Testing project support from India||Pega online job support from India|Pega project support from India|Pentaho online job support from India|Pentaho project support from India|Python online job support from India|Python project support from India
Keep on the good work and write more article like this...
Awesome Blog...It's very Useful for me!!! Thanks!!!
ReplyDeletePython Projects in Chennai
Python Project Centers in Chennai
Bigdata Projects in Chennai
Bigdata Project Centers in Chennai
Great Article. Thank you for sharing! Really an awesome post for every one.
ReplyDeleteProject Centers in Chennai
JavaScript Training in Chennai
Final Year Project Domains for IT
JavaScript Training in Chennai
Thanks for Sharing Wonderful blog!!!
ReplyDeletebest inplant training in chennai
inplant training in chennai
java training in chennai
core java training in chennai
advanced java training in chennai
struts training in chennai
android training in chennai
ReplyDeleteweb design training in chennai
web development training in chennai
seo training in chennai
digital marketing training in chennai
The Best Project Centers in Chennai
ReplyDeleteIeee 2019 Cloud Computing projects
Ieee 2019 Data Mining projects
Ieee 2019 Mobile Computing projects
Ieee 2019 Big data projects
Ieee 2019 Hadoop projects
eee project centers in chennai,engineering project centers in chennai,matlab project centers in chennai,embedded project centers in chennai
ReplyDeletebest ieee projects centre in chennai at affordable price
Visit :- http://ieeeprojectcentre.in/
Phone : +91 97518 00789 / +91 77081 50152
ReplyDeletems project centers in chennai Top Foundation ms project centers in Chennai, gives you a total (both fundamental and propelled) level of MS Task Preparing with life extension and venture the executive's ideas.
who provides Graphic services, web designing services, logo design services, graphic design
ReplyDeleteand all kind of website design,Graphic services.
graphic designer in delhi
freelance graphic designer in delhi
freelance graphic designer in delhi ncr
freelance graphic designer in noida
freelance logo designer in delhi
freelance logo designer in delhi ncr
freelance web designer in delhi ncr
freelance website designer in delhi ncr
freelance designer in delhi
freelance website designer in delhi
freelance web designer in delhi
freelance graphic designer services in delhi
freelancer graphic designer services in delhi ncr
freelancer graphic designer services in delhi
freelancer graphic services in delhi ncr
freelancer logo services in delhi
freelancer logo services in delhi ncr
freelancer web designer services in delhi ncr
freelancer web designer services in delhi
freelance web designer services in delhi
freelance website designer services in delhi
freelance website designer services in delhi ncr
freelance logo designer service in delhi
freelance logo designer service in delhi ncr
logo designer in delhi
brochure design in delhi
logo design in delhi
freelance logo design in delhi
freelance logo designer in gurgaon
freelance logo designer in noida
ReplyDeleterealtime project centers in chennai, college project centers in chennai, me project centers in chennai, mini project centers in chennai, matlab project centers in chennai It will likewise develop your insight and pragmatic involvement with different innovation and allow you to consider specific choice modules. ATS inserted frameworks mentors are implanted guaranteed specialists and experienced working experts with hand on ongoing Embedded framework venture information.
Please refer below if you are looking for best Online job support and proxy interview from India
ReplyDeleteDevOps Online Job Support From India | PHP Online Job Support From India | Selenium Online Job Support From India | Hadoop Online Job Support From India | Java Online Job Support From India | Angular Online Job Support From India | Python Online Job Support From India | Android Online Job Support From India
Thank you for excellent article.
Please refer below if you are looking for best Online job support and proxy interview from India
ReplyDeleteAWS Online Job Support From India | Workday Online Job Support From India | ReactJS Online Job Support From India | Manual Testing Online Job Support From India | Dotnet Online Job Support From India | Peoplesoft Online Job Support From India | Teradata Online Job Support From India
Thank you for excellent article.
vidmate
ReplyDeletePlease refer below if you are looking for best Online job support and proxy interview from India
ReplyDeleteDevOps Proxy Interview Support From India | PHP Proxy Interview Support From India | Selenium Proxy Interview Support From India | Hadoop Proxy Interview Support From India | Java Proxy Interview Support From India | Angular Proxy Interview Support From India | Python Proxy Interview Support From India | Android Proxy Interview Support From India
Thank you for excellent article.
Please refer below if you are looking for best Online job support and proxy interview from India
ReplyDeleteDevOps Proxy Interview Support From India | PHP Proxy Interview Support From India | Selenium Proxy Interview Support From India | Hadoop Proxy Interview Support From India | Java Proxy Interview Support From India | Angular Proxy Interview Support From India | Python Proxy Interview Support From India | Android Proxy Interview Support From India
Thank you for excellent article.
Please refer below if you are looking for best Online job support and proxy interview from India
ReplyDeleteAWS Proxy Interview Support From India | Workday Proxy Interview Support From India | ReactJS Proxy Interview Support From India | Manual Testing Proxy Interview Support From India | Dotnet Proxy Interview Support From India | Peoplesoft Proxy Interview Support From India | Teradata Proxy Interview Support From India
Thank you for excellent article.
Please refer below if you are looking for best Online job support and proxy interview from India
ReplyDeleteDevOps Online Job Support From India | PHP Online Job Support From India | Selenium Online Job Support From India | Hadoop Online Job Support From India | Java Online Job Support From India | Angular Online Job Support From India | Python Online Job Support From India | Android Online Job Support From India
Thank you for excellent article.
Download your favorite Latest Mp3 Lyrics which are available in English, Hindi, Bangla, Telugu, Latin, Arabic, Russian, etc.
ReplyDeleteClick Here
Click Here
Click Here
Click Here
Click Here
vidmate
ReplyDeletevidmate
ReplyDelete
ReplyDeleteClass College Education training Beauty teaching university academy lesson teacher master student spa manager skin care learn eyelash extensions tattoo spray
daythammynet
daythammynet
daythammynet
daythammynet
daythammynet
daythammynet
daythammynet
daythammynet
daythammynet
Thanks for provide great informatic and looking beautiful blog, really nice required information & the things i never imagined and i would request, wright more blog and blog post like that for us. Thanks you once agian
ReplyDeletecourt marriage in delhi ncr
court marriage in delhi
court marriage in noida
court marriage in ghaziabad
court marriage in gurgaon
court marriage in faridabad
court marriage in greater noida
name change online
court marriage in chandigarh
court marriage in bangalore
Thanks for posting
ReplyDeletemy website techwolk is related to technology
Thanks for posting
ReplyDeletemy webside techwolk.in/Sonsindiasong
I have Webside techwolk.in/Sonsindiasong for Related by All type Songs.
Also check for
Deletejava training in coimbatore
python training in coimbatore
Great blog !It is best institute.Top Training institute In chennai
ReplyDeletehttp://chennaitraining.in/oracle-dba-training-in-chennai/
http://chennaitraining.in/sql-server-dba-training-in-chennai/
http://chennaitraining.in/teradata-training-in-chennai/
http://chennaitraining.in/sap-hr-training-in-chennai/
http://chennaitraining.in/sap-fico-training-in-chennai/
http://chennaitraining.in/sap-abap-training-in-chennai/
It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
ReplyDeletehttp://chennaitraining.in/bi-bw-training-in-chennai/
http://chennaitraining.in/solidworks-training-in-chennai/
http://chennaitraining.in/autocad-training-in-chennai/
http://chennaitraining.in/ansys-training-in-chennai/
http://chennaitraining.in/revit-architecture-training-in-chennai/
http://chennaitraining.in/primavera-training-in-chennai/
Great blog !It is best institute.Top Training institute In chennai
ReplyDeletehttp://chennaitraining.in/oracle-dba-training-in-chennai/
http://chennaitraining.in/sql-server-dba-training-in-chennai/
http://chennaitraining.in/teradata-training-in-chennai/
http://chennaitraining.in/sap-hr-training-in-chennai/
http://chennaitraining.in/sap-fico-training-in-chennai/
http://chennaitraining.in/sap-abap-training-in-chennai/
vidmate download
ReplyDeletevidmate apk
Great blog !It is best institute.Top Training institute In chennai
ReplyDeletehttp://chennaitraining.in/openspan-training-in-chennai/
http://chennaitraining.in/uipath-training-in-chennai/
http://chennaitraining.in/automation-anywhere-training-in-chennai/
http://chennaitraining.in/microsoft-azure-training-in-chennai/
http://chennaitraining.in/workday-training-in-chennai/
http://chennaitraining.in/vmware-training-in-chennai/
Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care
ReplyDeletehttp://chennaitraining.in/php-training-in-chennai/
http://chennaitraining.in/nodejs-training-in-chennai/
http://chennaitraining.in/reactjs-training-in-chennai/
http://chennaitraining.in/ccnp-training-in-chennai/
http://chennaitraining.in/mcsa-training-in-chennai/
http://chennaitraining.in/mcse-training-in-chennai/
Great blog !It is best institute.Top Training institute In chennai
ReplyDeletehttp://chennaitraining.in/oracle-dba-training-in-chennai/
http://chennaitraining.in/sql-server-dba-training-in-chennai/
http://chennaitraining.in/teradata-training-in-chennai/
http://chennaitraining.in/sap-hr-training-in-chennai/
http://chennaitraining.in/sap-fico-training-in-chennai/
http://chennaitraining.in/sap-abap-training-in-chennai/
Download Latest Hindi & Punjabi Song Lyrics:
ReplyDeleteOye Hoye - Sukhe, Mellow D (Hindi)
Jinke Liye - Neha Kakkar, B Praak (HINDI)
Punjab Punjabi Zindabaad - Babbu Maan (HINDI)
Woow, Great information shared. I appreciate the persistence you put into your website and detailed information you provided.
ReplyDeleteAndroid app development
iOS App Development
lyricswhispering
ReplyDeleteHello... Thanks for the sharing your article with us... I hope your next article coming out soon.
ReplyDeleteBest 21 Motorcycle and Biker Quotes 2020 (English to Hindi)
Ambe Tu Hai Jagdambe Kali (Hindi Devotional Songs)
Best 30 Powerful Sad Quotes Translate English to Hindi
51 Best Love Quotes for Instagram Captions (English to Hindi)
Softyquotes
Great useful Information Its helps a lot to much useful For me...
ReplyDeleteNovels Tamil
Novels Tamil
Tamil Novels
Tamil Novels Blog
Latest Tamil Novels Free Download
Latest Tamil Novels Free Download
Latest Tamil Novels Free Download
Ramanichandran Novels
Ramanichandran Tamil Novels
Muthulakshmi Raghavan Novels
Srikala Novels
Thanks for sharing informative post. Keep on sharing such useful information. Looking for best tamil novels free download to improve your language skills, check out the popular novels below
ReplyDeleteMuthulakshmi Raghavan Novels
Mallika Manivannan Novels
Srikala Tamil Novels
Ramanichandran Novels
Sashi Murali novels
Rajesh Kumar Novels
Thanks for your information. very good article.
ReplyDeletePega certification
learn pega online
python online training in hyderabad
ReplyDeletepython online training hyderabad
mallumv
ReplyDeletetamilmv Tamil movie download 2021
Madras rockers
movieswood movie download
Thank you for sharing this valuable information with us.
ReplyDeleteTamil novels pdf download
Ramanichandran novels PDF
srikala novels PDF
Mallika manivannan novels PDF
muthulakshmi raghavan novels PDF
Infaa Alocious Novels PDF
N Seethalakshmi Novels PDF
Sashi Murali Tamil Novels PDF
Very nice blog
ReplyDeleteinternship request letter | Internship completion letter | internship companies | internship resume objective | internship application letter | Internship with training | internship email | internship experience | What internship means | Internship acknowledgement
I really like and appreciate your post.Really thank you! Fantastic.
ReplyDeletejava training
java online training
Awesome..You have clearly explained …Its very useful for me to know about new things..Keep on blogging..
ReplyDeleteartificial intelligence internship | best final year projects for cse | internship certificate online | internship for mba finance students | internship meaning in tamil
Thank u so much this really helped me Madras Rockers
ReplyDeleteSuperb blog and great post.Its truly supportive for me, anticipating for all the more new post. Continue Blogging!
ReplyDeleteBiotech Internships | internships for cse students | web designing course in chennai | it internships | electrical engineering internships | internship for bcom students | python training in chennai | web development internship | internship for bba students | internship for 1st year engineering students
iit organic chemistry
ReplyDeleteorganic chemistry tutor
organic chemistry teacher
Henrik Eichenhardt'S Blog: Why Double Checked Locking Is Broken? >>>>> Download Now
ReplyDelete>>>>> Download Full
Henrik Eichenhardt'S Blog: Why Double Checked Locking Is Broken? >>>>> Download LINK
>>>>> Download Now
Henrik Eichenhardt'S Blog: Why Double Checked Locking Is Broken? >>>>> Download Full
>>>>> Download LINK Vq
Good blog post
ReplyDeletehow to get job in canada for indian
Average Salary in Canada
CRS Calculator
Best immigration consultant in Delhi
Canada Point system