I found a presentation by Java Pune Bill Pew, Brian Goetz, and Cliff. Click on the topic of testing parallel code. They suggested this approach, because I think this is the best I've heard:
Several manufacturers create stateful and thread-safe objects with a state-dependent hashCode implementation. Since objects are transmitted through the intended synchronization point, the hash codes are summed (stream-locally). Similarly, consumers on the other side of the gate summarize the hash codes.
At the end of the test, we summarize all the results for manufacturers and consumers, respectively. If the two amounts are equal, the test passes.
We could also use XOR as an alternative to the sum. In fact, any commutative operation will be performed. Just keep in mind that the test wiring itself should not introduce any additional synchronization.
source share