1

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.

Matt Milna
  • 147
  • 7
  • Don't use system.exit as the way to terminate your program - it kills the process, rather than the thread. Depending on the rest of the program, you'll have to find a way to terminate the UI thread - how does the rest of the program initialise? – Ashley Frieze Apr 20 '15 at 19:35

6 Answers6

1

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Here is the relevant post for [JFrame.close() vs System.exit()](http://stackoverflow.com/questions/13360430/jframe-dispose-vs-system-exit) – John Apr 20 '15 at 19:51
0

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

Community
  • 1
  • 1
Alex Nevidomsky
  • 668
  • 7
  • 14
0

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

  • [Thread.stop() is unsafe!](https://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html) – John Apr 20 '15 at 19:39
  • @John yes, and as usual: if you'd read the **complete** post, you might notice the note mentioning you shouldn't use it -.- –  Apr 20 '15 at 19:45
  • I did read the complete post, if it shouldn't be used, why are suggesting it as a potential option? =| – John Apr 20 '15 at 19:46
  • "just for completeness" –  Apr 20 '15 at 19:49
0

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.

John
  • 3,769
  • 6
  • 30
  • 49
0

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.

Olivier Croisier
  • 6,139
  • 25
  • 34
0

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?

Mike Robinson
  • 8,490
  • 5
  • 28
  • 41