Testing concurrent code question 169 of 180 senior
What would you add to a code review checklist for concurrent code?
- That every shared method is synchronized
- Correct: For every mutable field, name the lock or edge that protects it; check compound actions, alien calls under locks, interrupt handling, timeouts and shutdown
- That the code uses
java.util.concurrent - That there are no
synchronizedkeywords
A checklist that finds real bugs:
- For each mutable field shared between threads, which lock or happens-before edge covers it, and is it documented?
- Is any sequence of individually safe calls a compound action that should be atomic?
- Is any unknown code (a listener, a callback, an overridable method) called while holding a lock?
- Is
InterruptedExceptionpropagated or the flag restored, never swallowed? - Does every blocking call have a timeout?
- Is every executor shut down, and is every
ThreadLocalremoved in afinally? - Are the collections chosen for the access pattern, not just for being thread safe?
- Do the tests bound their waits, and is the thread safety policy written in the javadoc?
Offering this list unprompted is usually a stronger signal than any single technical answer.
Quiz yourself on Testing concurrent code Suggest a correction