Dozer
Dozer

Reputation: 5225

System.exit in Java Thread

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?

PS. the new thread will call some method in other .jar file, so I can't modify it.

Upvotes: 6

Views: 15878

Answers (4)

Dozer
Dozer

Reputation: 5225

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

Upvotes: 4

bestsss
bestsss

Reputation: 12056

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

Upvotes: 5

Krushna
Krushna

Reputation: 6060

Use Java SecurityManager to save your main thread from exit and run the other thread code with the SecurityManager .

Edit: Take idea from Tomcat or other server how they menage the code like <% System.exit(1); %> this in a JSPs.

Upvotes: 0

Thihara
Thihara

Reputation: 6969

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....

Upvotes: 8

Related Questions