Java 线程:是否可以从运行在同一 JVM 上的不同 Java 程序查看/暂停/终止特定线程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/181615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java threads: Is it possible view/pause/kill a particular thread from a different java program running on the same JVM?
提问by Swanand
I have a program 'foo' running different threads, fooT1, fooT2, .. fooTn.
我有一个程序 'foo' 运行不同的线程,fooT1、fooT2、.. fooTn。
Now if I want write another program 'bar', which could kill the thread fooTr, is that possible?
现在,如果我想编写另一个程序“bar”,它可能会杀死线程 fooTr,这可能吗?
Reason: One of the thread fooTr tracks product license. If this thread is killed; one may run this product indefinitely. And killing 'foo' itself is tolerable as 'foo' as that is exactly what is being done on license expiry.
原因:线程之一 fooTr 跟踪产品许可。如果这个线程被杀死;一个人可以无限期地运行这个产品。杀死 'foo' 本身与 'foo' 一样是可以容忍的,因为这正是许可证到期时所做的事情。
System: Fedora Distribution of Linux
系统:Fedora Linux 发行版
Note: The commands which start JVM and program foo are placed in /etc/init.d and anyone who has a decent knowledge of rc.1/rc.2/rc.3 structure can change/add the starting parameters to these.
注意:启动 JVM 和程序 foo 的命令放在 /etc/init.d 中,任何对 rc.1/rc.2/rc.3 结构有一定了解的人都可以更改/添加这些启动参数。
I hope my question is clear. If not, I can always edit it.
我希望我的问题很清楚。如果没有,我可以随时编辑它。
采纳答案by Jaap Coomans
To my knowledge it is not possible to do this directly. What you could consider however is to create some kind of service on your 'foo' that can be called from 'bar' to kill the thread. There are, of course, hundreds of ways to implement this. My first thought would be to do this using RMI.
据我所知,无法直接执行此操作。但是,您可以考虑在您的“foo”上创建某种服务,可以从“bar”调用以终止线程。当然,有数百种方法可以实现这一点。我的第一个想法是使用RMI来做到这一点。
回答by l0st3d
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 调试器将允许您通过向其中注入异常来杀死线程。当我遇到这个问题时,我只是想弄清楚如何使用此功能来终止线程而不清除整个 jvm。如果您使用命令行选项运行 jvm,例如:
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 命令杀死正在运行的线程。我目前不确定的是这个 kill 命令的语法,我已经尝试了显而易见的:
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 ;)
(“Swank REPL 线程”是我想杀死的线程,是的,我已经尝试先挂起它;)
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.
我仍然无法使用 java 调试器,在我看来,线程可以被随机杀死。也许您可以确保忽略所有异常并继续运行就足够了,但我不确定这一点。
回答by Alexander
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.getAllStackTracesor Thread.enumerate), finds the offending thread, and invokes stop()on it. Although Thread.stopis deprecated, it still works.
即使没有单独的应用程序,您也可以做到这一点。编写您自己的启动类,该类将参数传递给应用程序的原始启动类。您的类的 main 方法虽然会创建一个线程,该线程会定期检查所有线程的列表(例如,Thread.getAllStackTraces或Thread.enumerate),找到有问题的线程,并对其进行调用stop()。虽然Thread.stop已被弃用,但它仍然有效。
Another option is to run the application under a Java debugger, say, jdband 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 jdbto the running JVM and suspect/kill the thread.
另一种选择是在 Java 调试器下运行应用程序,例如,jdb然后挂起/终止所需的线程。您还可以向应用程序的启动添加参数,以便可以附加 JVM,然后附加jdb到正在运行的 JVM 并怀疑/终止线程。
回答by Telcontar
Until now isn′t possible to run to diferent programs in the same JVM, but some people is investigating it, in order to reduce the startup time and the memory and cpu usage of diferent java programs runing in the same machine
直到现在还不能在同一个JVM上运行不同的程序,但有人正在研究它,以减少在同一台机器上运行的不同java程序的启动时间和内存和cpu的使用

![在 Java 正则表达式中,如何获取字符类(例如 [az])以匹配 - 减号?](/res/img/loading.gif)