下面的代码在Thread中增加一个静态变量,并检查其值是否增加1。这是Assert.assertEquals(currentAVal+1, accessCounter);检查的内容。

该测试持续通过了10,000次运行。但是,为什么没有竞争条件会导致测试失败?
我希望在断言发生之前,两个或多个线程在accessCounter行增加accessCounter = accessCounter + 1;,但这似乎没有发生?

public class RunnableTest {
    private static int accessCounter = 0;

    private class Post implements Runnable {
        public void run() {
            int currentAVal = accessCounter;
            accessCounter = accessCounter + 1;
            Assert.assertEquals(currentAVal+1, accessCounter);
            System.out.println("Access counter : "+accessCounter);
        }
    }

    @Test
    public void runTest(){
        Runnable r = new Post();
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(4);
        for(int executorCount = 0; executorCount < 10000; ++executorCount) {
            executor.execute(r);
        }
    }
}

更新:从格雷的答案我已经更新了代码,并且当我删除println语句时,我现在收到竞争条件(测试失败):
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.junit.Test;

import junit.framework.Assert;

public class RunnableTest {

    private static int accessCounter = 0;
    private static List<String> li = new ArrayList<String>();

    private class Post implements Runnable {
        public synchronized void run() {

            int currentAVal = accessCounter;
            accessCounter = accessCounter + 1;
            li.add(String.valueOf(currentAVal+1+","+accessCounter));

        }
    }

    @Test
    public void runTest(){

        Runnable r = new Post();
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(4);
        for(int executorCount = 0; executorCount < 10000; ++executorCount) {
            executor.execute(r);
        }
        //Wait for threads to finish
        // we shut it down once we've submitted all jobs to it
        executor.shutdown();
        // now we wait for all of those jobs to finish
        try {
            executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for(String s : li){
            Assert.assertEquals(s.split(",")[0], s.split(",")[1]);
        }
    }

}

synchronized添加到run方法会导致测试通过

最佳答案



竞争条件的定义是您可能会遇到计时问题-这是不能保证的。如果您在其他体系结构上运行此方法,则可能会得到截然不同的结果。

但是,我不认为junit可以看到其他线程中的断言。例如,如果我更改,请测试以下内容。我确实看到了有时值不同但测试方法看不到fail的情况-测试仍然通过。

if (currentAVal+1 != accessCounter) {
    System.out.println("Access counter not equal: "+accessCounter);
    Assert.fail();
}

您可能会在accessCounter中看到正确值的原因之一是System.out.println(...)是一种同步方法,它(作为副产品)同步accessCounter的值。

另外,您既不关闭执行程序,也不在等待执行程序服务实际完成。您应该执行以下操作:
// we shut it down once we've submitted all jobs to it
executor.shutdown();
// now we wait for all of those jobs to finish
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);

但这不能解决另一个线程问题。要实际查看线程的结果,您可以执行以下操作:
List<Future<?>> futures = new ArrayList<Future<?>>();
for (int executorCount = 0; executorCount < 10000; ++executorCount) {
    futures.add(executor.submit(r));
}
executor.shutdown();
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
for (Future<?> future : futures) {
    // this will throw an exception if an assert happened
    future.get();
}

09-25 20:59