如何使用junit执行并发测试?
How to use junit to run concurrency test?
假设我有一个类
public class MessageBoard { public synchronized void postMessage(String message) { .... } public void updateMessage(Long id, String message) { .... } }我要同时测试对这个postMessage的多个访问。 对此有什么建议?我想对所有我的setter函数(或任何涉及create / update / delete操作的方法)运行这种并发测试。
I wan to test multiple access to this postMessage concurrently. Any advice on this? I wish to run this kind of concurrency test against all my setter functions (or any method that involves create/update/delete operation).
推荐答案很遗憾,我不相信您可以通过运行时测试来确定您的代码是否是线程安全的。你可以抛出任意多的线程,因为它,它可能/可能不会通过,具体取决于调度。
Unfortunately I don't believe you can definitively prove that your code is thread-safe by using run-time testing. You can throw as many threads as you like against it, and it may/may not pass depending on the scheduling.
也许您应该查看一些静态分析工具,例如 PMD ,可以确定您如何使用同步和识别使用问题。
Perhaps you should look at some static analysis tools, such as PMD, that can determine how you're using synchronisation and identify usage problems.
更多推荐
如何运行并发单元测试?
发布评论