System.exit in Java Thread System.exit in Java Thread multithreading multithreading

System.exit in Java Thread


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.

That's the javadoc.

So the method will terminate the entire JVM. Not just the thread....


Your question is vastly unclear, yet if the System.exit call succeeds the OS will terminate your application.

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.

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.

link to similar question


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());