Any satisfactory approaches to unit testing thread safety in Java?

后端 未结 5 1589
孤街浪徒
孤街浪徒 2021-01-31 08:01

I am looking at improving a package that I believe not to be threadsafe when its input is shared between multiple worker threads. According to TDD principles, I should write so

5条回答
  •  滥情空心
    2021-01-31 08:39

    In some cases, I've found I can force a particular problematic interleaving of calls to a potentially un-thread-safe class by using multiple threads that synchronize with each other, perhaps with the use of CountDownLatch or some such concurrency mechanism. Sometimes this just doesn't work however, for instance if you're trying to test what happens if two threads are in the same method at the same time.

    Here's an interesting article (don't know much about the tool, though) : http://today.java.net/pub/a/today/2003/08/06/multithreadedTests.html

提交回复
热议问题