Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing thread-safety in Java [duplicate]

Possible Duplicate:
How should I unit test threaded code?
Guidelines for testing multithreaded code or ensuring that code is thread-safe

Is it possible to "unit test" whether a simple class is thread safe or not?

My specific case is a simple class, that subsets vectors: given a "white list" of vector positions to keep and an input vector, it produces an output vector with only the values at the positions in the white list. I'd like to write a unit test (if it is possible) to make sure that if we refactor this class in the future, we keep it thread safe. The unit test would fail if the class is no longer thread safe. I realize this is somewhat vague and under defined.

like image 502
Frank Avatar asked Aug 20 '12 15:08

Frank


1 Answers

Thread-safe is a property that a class has or doesn't have. Thread-safety can mean, depending on the context, absence of race conditions, the execution of code in a particular order or whatever else.

Testing cannot prove the absence of bugs, it can only reveal them. In my (not-so-vast) experience with ensuring that multithreaded code is correct probably the best tip is to keep the code as simple and clear as possible and try to discover bugs by inspection. Repeatedly running tests won't help too much.

like image 88
Tudor Avatar answered Sep 24 '22 13:09

Tudor