記一個低級錯誤

Java: jdk1.8.0_144java

作測試時犯下了一個極其低級的錯誤,記錄下來以警示本身。原計劃測試線程池運算的時間和預期值,代碼以下:安全

public class StudyCompletableFuture {
    private static int processors = Runtime.getRuntime().availableProcessors();
    static final int MAX_SIZE = 100000;
    private static final ExecutorService executorService = Executors.newFixedThreadPool(processors);

    public static Pair<Long, List<String>> executorService() throws InterruptedException {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        List<String> list = new ArrayList<>();
        for (int index = 0; index < MAX_SIZE; index++) {
            final String value = String.valueOf(index);
            executorService.execute(() -> {
                list.add(value);
            });
        }
        executorService.shutdown();
        executorService.awaitTermination(Integer.MAX_VALUE, TimeUnit.DAYS);
        stopWatch.stop();
        return ImmutablePair.of(stopWatch.getTime(), list);
    }

    @Test
    public void testExecutorService() throws Exception {
        Pair<Long, List<String>> pair = StudyCompletableFuture.executorService();
        System.out.println("executorService:" + pair.getLeft());
        Assert.assertEquals(StudyCompletableFuture.MAX_SIZE, pair.getRight().size());
    }
}

最終結果list的size老是不如預期,錯誤以下:eclipse

java.lang.AssertionError: expected:<100000> but was:<99746>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:834)
    at org.junit.Assert.assertEquals(Assert.java:645)
    at org.junit.Assert.assertEquals(Assert.java:631)
    at org.lxp.java8.StudyCompletableFuture.testExecutorService(StudyCompletableFuture.java:72)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

猜想一:ExecutorService丟失任務,致使少數thread未獲得執行機會測試

猜想二:ExecutorService在shutdown時拒絕接收新任務,致使少數thread未得獲得執行機會lua

最後在線程中記下日誌才現其實全部線程都獲得了執行機會,但由於使用了非線程安全的ArrayList致使返回值不符合預期,更換爲Vector後一切問題都完美解決,最終代碼以下:spa

public class StudyCompletableFuture {
    private static int processors = Runtime.getRuntime().availableProcessors();
    static final int MAX_SIZE = 100000;
    private static final ExecutorService executorService = Executors.newFixedThreadPool(processors);

    public static Pair<Long, List<String>> executorService() throws InterruptedException {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        List<String> list = new Vector<>();
        for (int index = 0; index < MAX_SIZE; index++) {
            final String value = String.valueOf(index);
            executorService.execute(() -> {
                list.add(value);
            });
        }
        executorService.shutdown();
        executorService.awaitTermination(Integer.MAX_VALUE, TimeUnit.DAYS);
        stopWatch.stop();
        return ImmutablePair.of(stopWatch.getTime(), list);
    }

    @Test
    public void testExecutorService() throws Exception {
        Pair<Long, List<String>> pair = StudyCompletableFuture.executorService();
        System.out.println("executorService:" + pair.getLeft());
        Assert.assertEquals(StudyCompletableFuture.MAX_SIZE, pair.getRight().size());
    }
}
相關文章
相關標籤/搜索