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

前端 未结 4 846
日久生厌
日久生厌 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:29

    Actually the java debugger will allow you to kill a thread by injecting an exception into it. I was just trying to work out how to use this feature to kill a thread without wiping out the whole jvm, when I came across this question. If you run the jvm with command line options like:

    java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8888 your.app.Main
    

    and connect the debugger with something like:

    jdb -attach 127.0.0.1:8888
    

    you can type:

    threads
    

    to get a list of the running threads, and use the kill command to kill a running thread. The bit I'm currently not sure about is the syntax of this kill command, I have tried the obvious:

    kill 0xe2e new java.lang.IllegalArgumentException("er");
    

    and I get the messages:

    killing thread: Swank REPL Thread
    Thread not suspended
    Expression must evaluate to an object
    

    ("Swank REPL Thread" is the thread I want to kill, and yes, I've tried suspending it first ;)

    Still my inability to use the java debugger aside, it looks to me like a thread can be killed at random. Maybe you can just make sure you ignore all exceptions and keep running and that will be enough, but I'm not sure about that.

提交回复
热议问题