I recently had a small contest with one of my colleagues (whom I really respect) regarding the theoretical ability to check if any code is thread safe or not.
Suppose we have some FooUnknown black box FooUnknown taken from a third-party library, so we donβt have access to its original source code. In addition, maybe he internally uses some of his own methods (if that is the question).
Can we write a unit test that tells us that using this class (for example, an instance shared between multiple threads) is 100% thread safe or not?
My conclusion is that it is impossible . For me, this is obvious and understandable: although you can write code that will lead to some concurrency problems that can be detected. But the lack of such results does not guarantee that there are no problems with concurrency at all.
I also think that this question is not too broad . To be sure, let's say that we have a class some.FooUnknown , and we want to use it as follows:
@ApplicationScoped public class FooService { private some.FooUnkown foo = new some.FooUnknown(); public void someStuff() {
How to check it to make sure that it is thread safe, that we do not need to wrap it in ThreadLocal<FooUnknown> ?
source share