/** Maintains a list of names. */
public class NameManager {
private List<String> names = new ArrayList<String>();
/** Stores a new list of names. This method is threadsafe. */
public void setNames(List<String> newNames) {
synchronized (names) {
names = new ArrayList<String>();
for (String name : newNames) {
names.add(name);
}
}
}
(Hint: the method setNames() is synchronized on the names field, but that field is then modified to point to a new object.)
OK, so spotting the bug was easy. But how would you write a Unit Test to demonstrate the problem? You would need to have two or more threads calling setNames() simultaneously, but you still don't have any control over how the threads will be scheduled.
Enter Thread Weaver, a test framework that lets you control thread execution. By setting breakpoints in your code, you can stop one thread at exactly the point that you want, and then allow a second thread to run. This allows you to write repeatable multi-threaded unit tests, without relying on the thread scheduler.
Ed. Note: Post updated with corrected formatting.
By Alasdair Mackintosh, Software Engineering Team