Concurrency Anti-pattern Catalog for Java

The sleep() anti-pattern

Problem: “The programmer assumes that a child thread should be faster than the parent thread in order that its results be available to the parent thread when it decides to advance. Therefore, the programmer sometimes adds an ?appropriate? sleep() to the parent thread. However, the parent thread may still be quicker in some environment." [FNU03].

Java example of the problem: ...

Context: Trying to coordinate threads based on assumptions regarding thread timing.

Solution: “The correct solution would be for the parent thread to use the join() method to explicitly wait for the child thread" [FNU03].

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.