Matt Milna
Matt Milna

Reputation: 145

Exiting one thread closes all

I have a java app which has a menu. One of the menu items is Exit. Which is defined as follows:

    item_exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {        
            System.exit(0);
        }
    });

Another menu item is New, which makes another instance (?) of the same program run in parallel. It is defined as follows:

    item_new.addActionListener(new ActionListener() {          
            MyApp app = new MyApp(); 
            app.start();
        }
    });

It works as desired except for one problem. It's that when I close one of them, both of them close. The entire app is built on one JFrame object. I don't think changing the default close operation of it will help. I think the issue is with system.exit(0). But, what is the alternative to fix this? I only want the thread I closed to close, not all of them. Thanks.

Upvotes: 1

Views: 962

Answers (6)

Mike Robinson
Mike Robinson

Reputation: 8945

Basically, you'll need to change the action of "Exit" so that it's able to detect when there's another "app" currently running ... and to do something plausible in that case. Maybe you alert the user that the other activity hasn't finished yet, and warn him that if he really wants to exit now, the other activity will be abandoned. Maybe you launch the other activity in such a way that it is altogether separate and therefore doesn't care. There are several good alternatives, so this really becomes a design decision on your part: what strategy makes the most intuitive sense for you, and for your users?

Upvotes: 0

Olivier Croisier
Olivier Croisier

Reputation: 6149

System.exit(0) shuts the whole JVM down. Your two instances run on the same JVM : you don't fork a process, you just create another instance of your MyApp class. So it is obvious that both "applications" will be killed.

Instead of calling System.exit(0), you should send a termination signal to the JFrame you want to close

frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));

This way, only that JFrame (or "application") will be killed.

Another option would be to fork a whole new process. For that, I recommend you take a look at the Process class.

Upvotes: 0

John
John

Reputation: 3807

Please read the documentation for System.exit(). It specifically states:

Terminates the currently running Java Virtual Machine.

This means the JVM will terminate, and all of your threads with it.

Upvotes: 0

user4668606
user4668606

Reputation:

System.exit(0) quits the complete program, not only the current thread. If you want to quit a thread, you have two options: a thread automatically quits as soon as the corresponding Runnables run() method finishes, or you can kill the thread using thread.stop() (just for completeness, shouldn't be used).

Upvotes: 0

Alex Nevidomsky
Alex Nevidomsky

Reputation: 698

System.exit(0); makes the entire Java machine to quit, with everything that is running within. Try not to use this, unless you really need this to happen.

For quitting, perhaps try checking this How to close a Java Swing application from the code

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691765

Creating an object and callings its start() method doesn't make another program run in parallel. It only creates an object, in the same JVM, and executes its start() method, in the same JVM.

System.exit() exits the JVM, so everything running in this JVM stops running.

To make a JFrame invisible, you call setVisible(false) on it. That won't stop the JVM.

Upvotes: 1

Related Questions