jdb可以杀死单个java线程吗?

发布于 2024-12-16 13:58:36 字数 555 浏览 4 评论 0原文

理论上,JDB(java 调试器)允许杀死单个线程。实践中,可能吗?这里我将 jdb 连接到我的 JVM,它在端口 8000 处有一个 agentlib:

$ jdb -attach 8000
> 

我列出了线程:

> threads
Group system:
  (java.lang ...
  [...]

我找到有问题的线程并尝试杀死它:

> kill 0x21bb new java.lang.Exception("die!")
killing thead: pool-766-thread-1
> com.sun.tools.example.debug.expr.ParseException: Unable to create java.lang.Exception instance
Expression must evaluate to an object

所以线程不能被杀死?

如何在 jdb 中创建一个新的异常,将其交给线程终止?

In theory, the JDB (java debugger) allows for a single thread to be killed. In practice, is it possible? Here I attach jdb to my JVM, which has an agentlib at port 8000:

$ jdb -attach 8000
> 

I list the threads:

> threads
Group system:
  (java.lang ...
  [...]

I find the thread in question and try to kill it:

> kill 0x21bb new java.lang.Exception("die!")
killing thead: pool-766-thread-1
> com.sun.tools.example.debug.expr.ParseException: Unable to create java.lang.Exception instance
Expression must evaluate to an object

So threads cannot be killed?

How can one create a new Exception in the jdb, to give it to the thread to die?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞笑我太脆弱 2024-12-23 13:58:36

以下

但是以下答案可能确实回答了您的问题...

The following answer on SO does seems only to enforce your question.

But the following answer probably does answer your question...

黯然#的苍凉 2024-12-23 13:58:36

您可能还运行 jconsole/jvisualvm。我遇到了同样的问题,但后来我关闭了 jconsole,jdb Kill 命令起作用了。

It is possible you have jconsole/jvisualvm also running. I had the same issue, but then I closed jconsole, the jdb kill command worked.

岁月打碎记忆 2024-12-23 13:58:36

我想我知道你的问题是什么。您不能使用jdb杀死线程,除非它们首先被挂起。但 JVM 中的一件奇怪的事情是你无法挂起正在睡眠的线程。如果它是“运行(在断点处)”,这实际上也意味着挂起,那么它可以被杀死:

main[1] threads
Group system:
  (java.lang.ref.Reference$ReferenceHandler)0x162 Reference Handler cond. waiting
  (java.lang.ref.Finalizer$FinalizerThread)0x161  Finalizer         cond. waiting
  (java.lang.Thread)0x160                         Signal Dispatcher running
Group main:
  (java.lang.Thread)0x1                           main              running (at breakpoint)
main[1] kill 0x1 new java.lang.Exception("die!")
killing thread: main
main[1] instance of java.lang.Thread(name='main', id=1) killed

OTOH,如果你试图杀死一个正在睡眠的线程,这似乎是不可能的......

> threads
Group system:
  (java.lang.ref.Reference$ReferenceHandler)0x162 Reference Handler cond. waiting
  (java.lang.ref.Finalizer$FinalizerThread)0x161  Finalizer         cond. waiting
  (java.lang.Thread)0x160                         Signal Dispatcher running
Group main:
  (java.lang.Thread)0x1bf                         Thread-0          sleeping
  (java.lang.Thread)0x1c1                         DestroyJavaVM     running
> kill 0x1bf new java.lang.Exception("die!")
killing thread: Thread-0
> com.sun.tools.example.debug.expr.ParseException: Unable to create java.lang.Exception instance
Expression must evaluate to an object
> suspend
All threads suspended.
> kill 0x1bf new java.lang.Exception("die!")
killing thread: Thread-0
> com.sun.tools.example.debug.expr.ParseException: Unable to create java.lang.Exception instance
Expression must evaluate to an object
> threads
Group system:
  (java.lang.ref.Reference$ReferenceHandler)0x162 Reference Handler cond. waiting
  (java.lang.ref.Finalizer$FinalizerThread)0x161  Finalizer         cond. waiting
  (java.lang.Thread)0x160                         Signal Dispatcher running
Group main:
  (java.lang.Thread)0x1bf                         Thread-0          sleeping
  (java.lang.Thread)0x1c1                         DestroyJavaVM     running
> 

甚至切换到那个(睡眠)线程的自己的堆栈框架并不完全有帮助

> thread 0x1bf
Thread-0[1] kill 0x1bf new java.lang.Exception("die!")
killing thread: Thread-0
Thread-0[1] Thread not suspended
Expression must evaluate to an object

另请注意 JDWP 特定线程 不能 就这样被杀了(通过 JDI,jdb 也在下面使用),因为 libjdwp 会在请求时出错。鉴于您的示例中涉及的线程名称pool-766-thread-1,我不认为它是那些(JDWP)线程之一。

I think I know what your problem was. You cannot kill threads with jdb unless they are first suspended. But one of the strange things in JVM is that you cannot suspend a sleeping thread. If it's "running (at breakpoint)", which really also means suspended, then it can be killed:

main[1] threads
Group system:
  (java.lang.ref.Reference$ReferenceHandler)0x162 Reference Handler cond. waiting
  (java.lang.ref.Finalizer$FinalizerThread)0x161  Finalizer         cond. waiting
  (java.lang.Thread)0x160                         Signal Dispatcher running
Group main:
  (java.lang.Thread)0x1                           main              running (at breakpoint)
main[1] kill 0x1 new java.lang.Exception("die!")
killing thread: main
main[1] instance of java.lang.Thread(name='main', id=1) killed

OTOH, if you're trying to kill a thread that's sleeping, that seems impossible...

> threads
Group system:
  (java.lang.ref.Reference$ReferenceHandler)0x162 Reference Handler cond. waiting
  (java.lang.ref.Finalizer$FinalizerThread)0x161  Finalizer         cond. waiting
  (java.lang.Thread)0x160                         Signal Dispatcher running
Group main:
  (java.lang.Thread)0x1bf                         Thread-0          sleeping
  (java.lang.Thread)0x1c1                         DestroyJavaVM     running
> kill 0x1bf new java.lang.Exception("die!")
killing thread: Thread-0
> com.sun.tools.example.debug.expr.ParseException: Unable to create java.lang.Exception instance
Expression must evaluate to an object
> suspend
All threads suspended.
> kill 0x1bf new java.lang.Exception("die!")
killing thread: Thread-0
> com.sun.tools.example.debug.expr.ParseException: Unable to create java.lang.Exception instance
Expression must evaluate to an object
> threads
Group system:
  (java.lang.ref.Reference$ReferenceHandler)0x162 Reference Handler cond. waiting
  (java.lang.ref.Finalizer$FinalizerThread)0x161  Finalizer         cond. waiting
  (java.lang.Thread)0x160                         Signal Dispatcher running
Group main:
  (java.lang.Thread)0x1bf                         Thread-0          sleeping
  (java.lang.Thread)0x1c1                         DestroyJavaVM     running
> 

Even switching to that (sleeping) thread's own stack frame doesn't exactly help

> thread 0x1bf
Thread-0[1] kill 0x1bf new java.lang.Exception("die!")
killing thread: Thread-0
Thread-0[1] Thread not suspended
Expression must evaluate to an object

Also note that JDWP specific threads cannot be killed that way at all (via JDI, which jdb is also using underneath), as libjdwp will err on the request. Given the thread name pool-766-thread-1 involved in your example, I don't think it was one of those (JDWP) threads though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文