我有以下代码。显然,Reference 类不是线程安全的,因为它不保护它的“reference”属性。我如何证明我需要通过例如 Atomicreference 来保护它?
当我运行以下 JUnit 测试时,它在两个 Windows 上都成功:Intel(R) Core(TM) i5-2400 CPU @ 3.10GHz 和 Linux:Intel(R) Xeon(R) CPU X5670 @ 2.93GHz,使用 JRE 1.7.0_15。
import java.util.concurrent.CountDownLatch;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class AssignReferenceTest {
private static class Reference {
private Object reference = null;
private void setReference(Object reference) {
this.reference = reference;
}
boolean hasReference() {
return reference != null;
}
}
@Test
public void runManyTimes() throws Exception {
for (int i = 0; i < 100000; i++) {
testReferenceVisibilityProblem();
}
}
public void testReferenceVisibilityProblem() throws Exception {
final Reference reference = new Reference();
final CountDownLatch latch = new CountDownLatch(1);
Thread writeThread = new Thread(new Runnable() {
public void run() {
reference.setReference(new Object());
latch.countDown();
}
});
Thread readThread = new Thread(new Runnable() {
public void run() {
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
assertTrue("Should have the reference", reference.hasReference());
}
});
writeThread.start();
readThread.start();
writeThread.join();
readThread.join();
}
}