Java 线程中的 System.exit
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15945436/
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
System.exit in Java Thread
提问by Dozer
My main thread created a new thread
And when the new thread call System.exit(-1)
,my main thread be closed.
How can I handle the exit code and keep the main thread alive?
我的主线程创建了一个新线程,当新线程调用时System.exit(-1)
,我的主线程被关闭。如何处理退出代码并使主线程保持活动状态?
PS.
the new thread will call some method in other .jar
file, so I can't modify it.
附注。新线程将调用其他.jar
文件中的某些方法,因此我无法修改它。
采纳答案by Dozer
the class:
班上:
public class MySecurityManager extends SecurityManager {
@Override
public void checkPermission(Permission perm) {
}
@Override
public void checkPermission(Permission perm, Object context) {
}
@Override
public void checkCreateClassLoader() {
}
@Override
public void checkAccess(Thread t) {
}
@Override
public void checkAccess(ThreadGroup g) {
}
@Override
public void checkExit(int status) {
throw new SecurityException("not allow to call System.exit");
}
@Override
public void checkExec(String cmd) {
}
@Override
public void checkLink(String lib) {
}
@Override
public void checkRead(FileDescriptor fd) {
}
@Override
public void checkRead(String file) {
}
@Override
public void checkRead(String file, Object context) {
}
@Override
public void checkWrite(FileDescriptor fd) {
}
@Override
public void checkWrite(String file) {
}
@Override
public void checkDelete(String file) {
}
@Override
public void checkConnect(String host, int port) {
}
@Override
public void checkConnect(String host, int port, Object context) {
}
@Override
public void checkListen(int port) {
}
@Override
public void checkAccept(String host, int port) {
}
@Override
public void checkMulticast(InetAddress maddr) {
}
@Override
public void checkPropertiesAccess() {
}
@Override
public void checkPropertyAccess(String key) {
}
@Override
public boolean checkTopLevelWindow(Object window) {
return super.checkTopLevelWindow(window);
}
@Override
public void checkPrintJobAccess() {
}
@Override
public void checkSystemClipboardAccess() {
}
@Override
public void checkAwtEventQueueAccess() {
}
@Override
public void checkPackageAccess(String pkg) {
}
@Override
public void checkPackageDefinition(String pkg) {
}
@Override
public void checkSetFactory() {
}
@Override
public void checkMemberAccess(Class<?> clazz, int which) {
}
@Override
public void checkSecurityAccess(String target) {
}
}
on startop:
在启动时:
System.setSecurityManager(new MySecurityManager());
回答by Thihara
You can't.
你不能。
Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
That's the javadoc.
那就是javadoc。
So the method will terminate the entire JVM. Not just the thread....
因此该方法将终止整个 JVM。不仅仅是线程......
回答by bestsss
Your question is vastly unclear, yet if the System.exit call succeeds the OS will terminate your application.
您的问题非常不清楚,但如果 System.exit 调用成功,操作系统将终止您的应用程序。
If you wish System.exit not to succeed you can install a Security manager and prevent that. Other than that you can instrument the code via custom classloader and remove the call.
如果您不希望 System.exit 成功,您可以安装安全管理器并阻止它。除此之外,您可以通过自定义类加载器检测代码并删除调用。
Edit: if you go w/ Security manager, most likely throwing the SecurityException will terminate the thread. If it doesn't - cheat and throw a ThreadDeath. If that still doesn't - just hold the thread e.g. for(;;) Thread.sleep(10000);
The latter will leak the thread and its resources but at least won't kill your application.
编辑:如果您使用安全管理器,很可能抛出 SecurityException 将终止线程。如果没有 - 作弊并抛出 ThreadDeath。如果仍然没有 - 只需保持线程,例如for(;;) Thread.sleep(10000);
后者将泄漏线程及其资源,但至少不会杀死您的应用程序。
回答by Krushna
Use Java SecurityManager to save your main thread from exit and run the other thread code with the SecurityManager .
使用 Java SecurityManager 来保存您的主线程退出并使用 SecurityManager 运行其他线程代码。
Edit:
Take idea from Tomcat or other server how they menage the code like <% System.exit(1); %>
this in a JSPs.
编辑:从 Tomcat 或其他服务器获取他们如何<% System.exit(1); %>
在 JSP 中管理这样的代码的想法。