ExecutorService在JUnit测试中不执行Runnable [英] ExecutorService doesn't execute Runnables in JUnit tests

查看:92
本文介绍了ExecutorService在JUnit测试中不执行Runnable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要测试的执行器服务.

I have an executor service that I want to test.

我需要调用的任务(Runnables)很少,然后调用 wait(),并且虚拟计时器在几秒钟后调用 notify().

I have few tasks (Runnables) which need to be called, then call wait() and a dummy timer calls notify() after few seconds.

我这样做是为了检查所有任务是否按顺序执行并在一段时间后执行.

I've made this to check if all tasks are executed in sequence and after some periods of time.

问题是我的Runnable的 run()方法根本没有被调用.我已经设置了一个断点,但是代码执行没有达到它.

The problem is that run() method of my Runnable is not called at all. I've setup a breakpoint and the code execution doesn't reach it.

在普通编码中效果很好.任务排队,它们调用 wait(),直到未获得某些响应或触发超时,然后它们调用 notify(),然后执行下一个任务.

In plain coding it works well. Tasks are queued, they call wait() until some response is not gained or timeout is triggered, they call notify() and next task is executed.

问题是当我运行测试时.

The problem is when I run tests.

对此有什么想法吗?

推荐答案

test 方法在其他线程达到 wait notify .

This could happen when the test method exits before other threads hit wait and notify.

尝试致电:

executorService.shutdown();
try {
    executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {

}

在退出 test 方法之前.它将阻塞测试方法,直到所有 Runnable 完成.

before exits the test method. It will block the test method until all Runnable finish.

这篇关于ExecutorService在JUnit测试中不执行Runnable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆