System.exit in Java Thread

My main thread created a new thread And when the new thread calls System.exit(-1) , my main thread will be closed. How can I process the exit code and keep the main thread alive?

PS. the new thread will call some method in another .jar file, so I cannot change it.

+6
source share
4 answers

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) { } } 


at startup:

 System.setSecurityManager(new MySecurityManager()); 
+2
source

You can not.

Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

This is javadoc.

Thus, the method will complete the entire JVM. Not just a stream ....

+5
source

Your question is very unclear, but if the call to System.exit succeeds, the OS will terminate your application.

If you want System.exit to fail, you can install the security manager and prevent this. In addition, you can process the code through a custom class loader and remove the call.

Edit: if you run w / Security manager, most likely throwing a SecurityException, this will stop the thread. If this is not the case, cheat and quit ThreadDeath. If this is not the case, just hold the thread, for example. for(;;) Thread.sleep(10000); The latter flows through the thread and its resources, but at least it won’t kill your application.

link to a similar question

+5
source

Use the Java SecurityManager to keep the main thread from exiting and run another thread code using the SecurityManager.

Edit: Take an idea from Tomcat or another server how they use code like <% System.exit(1); %> <% System.exit(1); %> in JSP.

0
source

All Articles