为什么我不能证明我的代码中需要AtomicReference?

时间:2013-08-21 13:58:36

标签: java multithreading

我有以下代码。显然,Reference类不是线程安全的,因为它不保护其“引用”属性。我如何证明我需要通过例如Atomicreference来保护它?

当我运行以下JUnit测试时,它会在两个Windows上运行:Intel(R)Core(TM)i5-2400 CPU @ 3.10GHz和Linux:使用JRE 1.7的Intel(R)Xeon(R)CPU X5670 @ 2.93GHz .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();
    }
}

1 个答案:

答案 0 :(得分:4)

您的代码是线程安全的,因为CountDownLatch保证在await()之前完成的每个更改都会在之后完成所有操作之前完成。

请参阅http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/package-summary.html

  

在“释放”同步器方法之前的操作,例如Lock.unlock,Semaphore.release和CountDownLatch.countDown,在成功的“获取”方法(例如Lock.lock,Semaphore.acquire,Condition.await)之后的操作之前发生。和另一个线程中同一个同步器对象上的CountDownLatch.await。