I was reminded of the "On Demand Holder" idiom the other day. If you're not familiar with it, the idiom is a thread safe replacement for lazy initialization using double checked locking. As we all know double checked locking is broken. Here is an example implementation -
This works because of the way classes are loaded. The inner class Holder is not loaded and initialized until a thread references it, so the static instance of Something is not created until the first time that the getInstance() method is called. Here is more information and references for "On Demand Holder".
public class Something {
private Something() { }
private static class Holder {
private static final Something instance = new Something();
}
public static Something getInstance() {
return Holder.instance;
}
}
This works because of the way classes are loaded. The inner class Holder is not loaded and initialized until a thread references it, so the static instance of Something is not created until the first time that the getInstance() method is called. Here is more information and references for "On Demand Holder".
6 comments:
Thanks a lot for talking about useful survey. It necessary to understand that mobile apps development could help in your business by installing customized software solutions. Also you may turn your attention on offshore sql server development.
That is nice posts. Let me mention about home insurance with discounts homeowners insurance providers. Save on online free quotes on homeowners insurance.
Thank you for writing this useful review. You have nice possibility to clarify gaming affiliate. The most common casino programs such as 32Red and great poker rooms such as redbet affiliates.
We're glad to read about this. Here we are willing to share with insurance instant rates that is provided by major insurance companies. You may save on homeowners insurance quotes which allow customers to apply for affordable cheap policy.
A very good and informative article indeed. It helps me a lot to enhance my knowledge more, I really like the way you explain how it goes.
click here
I found your website perfect for my needs. It contains wonderful and helpful posts. I have read most of them and got a lot from them. To me, you are doing the great work.
Post a Comment