Sunday, July 15, 2007

Lazy Initialization Using an On Demand Holder

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 -
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:

Joshua Smith said...

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.

Joshua Smith said...

That is nice posts. Let me mention about home insurance with discounts homeowners insurance providers. Save on online free quotes on homeowners insurance.

Joshua Smith said...

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.

Joshua Smith said...

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.

Riyaad said...



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

home page said...

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.