How to kill a java thread using VisualVM or using a unix command?

后端 未结 8 1108
耶瑟儿~
耶瑟儿~ 2021-01-31 04:30

I m using windows 7 OS. I have around 6 threads in my application. For the purpose of testing the alerts to check the health of the threads, i need to kill the thre

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-31 05:13

    As Peter says, you can't do this safely. Indeed on some platforms Thread.kill is not even implemented. However:

    • If this is just for testing, a unit test that called Thread.kill would be reasonable ... assuming it worked on the test platforms where it needed to work. (A "loud" comment in the source code would be in order to help people porting the unit test ...)

    • Another alternative is to add some code to the thread runnable that allows your unit tests to tell it to die. If the thread code needs to be (almost) production code for this to work, you could create a subclass that overrides something so that it "breaks" in a way that suits your purposes ... for testing. In fact, this approach allows you to cause the threads "break" in controlled ways, potentially allowing you to test different aspects of your alerting code.

提交回复
热议问题