在Java中测试线程安全性[重复]

时间:2021-10-15 21:00:11

Possible Duplicate:
How should I unit test threaded code?
Guidelines for testing multithreaded code or ensuring that code is thread-safe

可能重复:我应该如何对线程代码进行单元测试?测试多线程代码或确保代码是线程安全的准则

Is it possible to "unit test" whether a simple class is thread safe or not?

是否可以“单元测试”一个简单的类是否是线程安全的?

My specific case is a simple class, that subsets vectors: given a "white list" of vector positions to keep and an input vector, it produces an output vector with only the values at the positions in the white list. I'd like to write a unit test (if it is possible) to make sure that if we refactor this class in the future, we keep it thread safe. The unit test would fail if the class is no longer thread safe. I realize this is somewhat vague and under defined.

我的具体情况是一个简单的类,即子集向量:给定要保持的向量位置的“白名单”和输入向量,它产生一个输出向量,其中只有白名单中位置的值。我想写一个单元测试(如果可能的话)以确保如果我们将来重构这个类,我们保持它的线程安全。如果类不再是线程安全的,单元测试将失败。我意识到这有点模糊,并且在定义之下。

2 个解决方案

#1


5  

Thread-safe is a property that a class has or doesn't have. Thread-safety can mean, depending on the context, absence of race conditions, the execution of code in a particular order or whatever else.

线程安全是类具有或不具有的属性。线程安全可能意味着,取决于上下文,没有竞争条件,以特定顺序执行代码或其他任何事情。

Testing cannot prove the absence of bugs, it can only reveal them. In my (not-so-vast) experience with ensuring that multithreaded code is correct probably the best tip is to keep the code as simple and clear as possible and try to discover bugs by inspection. Repeatedly running tests won't help too much.

测试不能证明没有错误,它只能揭示它们。在确保多线程代码正确的(不那么庞大)经验中,最好的建议是尽可能保持代码简单明了,并尝试通过检查发现错误。反复运行测试无济于事。

#2


4  

This is exactly what Java Pathfinder is for. It has a bit of a steep learning curve, but it's actually possible to build exhaustive proofs with this tool. You build a scenario and run it with JPF, then JPF explores all possible thread orderings to find possible errors. You need to build assertions into your program for JPF to check. JPF assumes sequential consistency during execution, but you can use the Java Racefinder plugin to prove that too.

这正是Java Pathfinder的用途。它有一些陡峭的学习曲线,但实际上可以用这个工具建立详尽的证明。您构建一个场景并使用JPF运行它,然后JPF探索所有可能的线程排序以查找可能的错误。您需要在程序中构建断言以供JPF检查。 JPF假定执行期间的顺序一致性,但您也可以使用Java Racefinder插件来证明这一点。

Admittedly, it's hard to build a proper proof—but it is possible. If nothing else JPF can be used to help you root out some concurrency errors that you might otherwise miss.

不可否认,很难建立一个合适的证据 - 但这是可能的。如果没有别的东西可以用JPF来帮助你找出一些你可能会错过的并发错误。

#1


5  

Thread-safe is a property that a class has or doesn't have. Thread-safety can mean, depending on the context, absence of race conditions, the execution of code in a particular order or whatever else.

线程安全是类具有或不具有的属性。线程安全可能意味着,取决于上下文,没有竞争条件,以特定顺序执行代码或其他任何事情。

Testing cannot prove the absence of bugs, it can only reveal them. In my (not-so-vast) experience with ensuring that multithreaded code is correct probably the best tip is to keep the code as simple and clear as possible and try to discover bugs by inspection. Repeatedly running tests won't help too much.

测试不能证明没有错误,它只能揭示它们。在确保多线程代码正确的(不那么庞大)经验中,最好的建议是尽可能保持代码简单明了,并尝试通过检查发现错误。反复运行测试无济于事。

#2


4  

This is exactly what Java Pathfinder is for. It has a bit of a steep learning curve, but it's actually possible to build exhaustive proofs with this tool. You build a scenario and run it with JPF, then JPF explores all possible thread orderings to find possible errors. You need to build assertions into your program for JPF to check. JPF assumes sequential consistency during execution, but you can use the Java Racefinder plugin to prove that too.

这正是Java Pathfinder的用途。它有一些陡峭的学习曲线,但实际上可以用这个工具建立详尽的证明。您构建一个场景并使用JPF运行它,然后JPF探索所有可能的线程排序以查找可能的错误。您需要在程序中构建断言以供JPF检查。 JPF假定执行期间的顺序一致性,但您也可以使用Java Racefinder插件来证明这一点。

Admittedly, it's hard to build a proper proof—but it is possible. If nothing else JPF can be used to help you root out some concurrency errors that you might otherwise miss.

不可否认,很难建立一个合适的证据 - 但这是可能的。如果没有别的东西可以用JPF来帮助你找出一些你可能会错过的并发错误。