Java threads: Is it possible view/pause/kill a particular thread from a different java program running on the same JVM?

前端 未结 4 758
日久生厌
日久生厌 2020-12-10 07:15

I have a program \'foo\' running different threads, fooT1, fooT2, .. fooTn.

Now if I want write another program \'bar\', which could kill the thread fooTr, is that p

4条回答
  •  醉梦人生
    2020-12-10 07:45

    You could do this even without a separate application. Write your own startup class, which performs a pass-through of parameters to the original startup class of the application. Your class's main method though would create a thread that periodically checks the list of all threads (e.g., Thread.getAllStackTraces or Thread.enumerate), finds the offending thread, and invokes stop() on it. Although Thread.stop is deprecated, it still works.

    Another option is to run the application under a Java debugger, say, jdb and then suspend/kill the required thread. You could also add parameters to the application's startup so that the JVM can be attached to, then attach jdb to the running JVM and suspect/kill the thread.

提交回复
热议问题