How to kill a java thread?

前端 未结 8 1976
一个人的身影
一个人的身影 2020-12-11 08:26

I google the solution for killing a java thread. And there exists two solutions:

  1. set a flag
  2. using Thread.interrupt

But both of them ar

8条回答
  •  爱一瞬间的悲伤
    2020-12-11 09:00

    You can invoke the stop method on the Thread object but it is strongly recommended that you don't. Read this: http://download.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html

    Do you have the freedom to make minor changes to the API? Is this blocking call CPU bound or IO bound? If it is IO bound and if you have access to the underlying socket/remote communication object, closing that object can do wonders. It is at least better than stopping the thread.

提交回复
热议问题