Concurrency Anti-pattern Catalog for Java

Double-checked lock anti-pattern

Problem: “When an object is initialized, the thread local copy of the objects field is initialized but not all object fields are necessarily written to the heap. This might cause the object to be partially initialized while its reference is not null” [FNU03].

Java example of the problem: ...

Context:Trying to initialize shared variables without using protection.

Solution: Use locks to synchronize all access to the object or use volatile. Do not perform lazy initialization on shared objects.

Java example of the solution: ...

References:

[FNU03] E. Farchi, Y. Nir, and S. Ur, “Concurrent bug patterns and how to test them,” in Proc. of the 1st International Workshop on Parallel and Distributed Systems: Testing and Debugging (PADTAD 2003), Apr. 2003.