Java: jdk1.8.0_144
做测试时犯下了一个极其低级的错误,记录下来以警示自己。原计划测试线程池运算的时间和预期值,代码如下:
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总是不如预期,错误如下:
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未得得到执行机会
最后在线程中记下日志才现其实所有线程都得到了执行机会,但因为使用了非线程安全的ArrayList导致返回值不符合预期,更换为Vector后一切问题都完美解决,最终代码如下:
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()); } }