java - spring boot的测试问题,求解答
大家讲道理
大家讲道理 2017-04-18 10:32:50
[Java讨论组]

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@Transactional
@SpringApplicationConfiguration(Application.class)
public class Main {

static Log logger = LogFactory.getLog(Main.class);

@Test
public void test() {        
    ExecutorService fixedThreadPool = Executors.newFixedThreadPool(11);
    for (int i = 0; i < 10; i++) {
        fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("a");
            }
            
        });
    }
    System.out.println("b");
}

为什么输出结果是ab,而不是10个a和1个b

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(1)
怪我咯

单元测试运行结束后会强制退出进程。所以剩下的线程并不会执行。你可以使用下面的示例等待所有任务执行完成。

ExecutorService executorService = Executors.newFixedThreadPool(11);
CountDownLatch latch = new CountDownLatch(10);
for (int i = 0; i < 10; i++) {
    final int v = i;
    executorService.execute(() -> {
        latch.countDown();
        System.out.println(v + " - a");
    });
}
System.out.println("b");
try {
    // 等待所有任务结束
    latch.await();
} catch (InterruptedException e) {
    e.printStackTrace();
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号