Cezar's reflections

Saturday, August 08, 2009

The double checked locking - Broken

If you're thinking of using the following code to optimize thread contention in java, don't!


class Foo
{
private Helper helper = null;

public Helper getHelper()
{
if (helper == null)
{
synchronized(this)
{
if (helper == null)
{
helper = new Helper();
}
}
}

return helper;
}
}
Read the following posts first:

Smart Ideas

Have a new smart idea, run it through this article and reevaluate it: part 1 and part 2.