Archive

Archive for October, 2009

Thread-safe singleton

October 11, 2009 Leave a comment

It seems that the better way to implement a thread safe singleton in java is using an holder that solves the lazy initialization too:

public class ConfigurationRegistry
{
private ConfigurationRegistry()
{
}

public static ConfigurationRegistry getInstance()
{
//Holder.instance is lazy initialized the first time access...
return Holder.instance;
}

private static class Holder
{
//single line thread safe
static final ConfigurationRegistry instance = new ConfigurationRegistry();
}

}
Categories: Uncategorized