Any satisfactory approaches to unit testing thread safety in Java?

后端 未结 5 1680
孤街浪徒
孤街浪徒 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:28

    Forget getting good results by testing for concurrency problems. Attempt to reduce synchronization and make the problem smaller. Then use as high as possible library support to do synchronization. And only if you really have then try to handle the concurrency yourself. When you know each worker does its work correct and all your thinking tell you that you have the concurrency problem licked then generate some interesting load. Unittest frameworks and their extensions can do that job, but know that you are not testing any units anymore. (Remember, you already had that part covered)

    If your concurrency model gets complicated check out tools suited for that like SPIN.

提交回复
热议问题