Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

System.exit(0) necessary after JOptionPane???

 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another question: my book says I must use System.exit(0) after using JOptionPane, but I've constructed a few programs now without using this call, and they work fine. In what cases must I use System.exit(0), then? Thanks in advance!

-Patrick
 
Campbell Ritchie
Sheriff
Pie
Posts: 48976
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The usual explanation is the every time you use JOptionPane you start a new Thread; using System.exit(0) when the application has completely finished ensures that the JVM exits and all those Threads are actually killed.
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so, coming from C++, I guess one could liken it to destroying a pointer after use, to free up memory?

-Patrick
 
Campbell Ritchie
Sheriff
Pie
Posts: 48976
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Patrick Brooks:
OK, so, coming from C++, I guess one could liken it to destroying a pointer after use, to free up memory?
No. I am not sure quite how essential the instruction is, but it is not at all like freeing a pointer. There is no explicit memory freeing action in Java at all; memory management is all done behind the scenes and users are barely aware of it.

Use of System.exit(0) in this instance is intended to make sure the virtual machine shuts down properly.
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification!

-Patrick
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A book says you MUST use System.exit() after using a JOptionPane?! Either the context is very different or that's a very bad book.

System.exit() is very rarely really needed. A Java program will exit just fine, if you ensure that all threads exit (i.e. the method in which they started returns).

Using System.exit() liberally throughout your code is bad for code re-use. Say you write a method and put System.exit() in its error handling, because the application in which you first use that method wants to exit on error. But then later you want to re-use the method in a Web application. Your System.exit() will kill the whole Web server, when really you just wanted it to log a problem and carry on.

An additional problem with System.exit() occurs in any multi-threaded application. System.exit() kills all threads pretty much straight away. But some threads might be doing something critical like updating a file. The file could get corrupted if thread is killed at a bad time. Instead, all threads should be allowed to finish in their own time. An application should have some way of signalling to all threads that shut-down is wanted, then they can shut-down safely.

The specific issue with JOptionPane may be one that afflicts all Swing (and AWT?) graphical applications. A Swing application will not exit until all windows are disposed. But if you create a top-level window without specifying any parent for it, Java will create a Parent that you can't control and hence never gets disposed. Instead, when creating a top-level window, you should give an explicit parent, such as an invisible JFrame. When you want to exit, you should dispose() the invisible JFrame, then the app will exit.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
The usual explanation is the every time you use JOptionPane you start a new Thread


I'm a bit rusty on this, but am fairly sure that's not true.

When you first use Swing code in a Java application, a thread (or threads?) get started to service the Swing subsystem. But I'm pretty sure you don't get an additional thread for every JOptionPane.

See my earlier response for why people sometimes use System.exit() in Swing applications, why they mostly shouldn't, and what they should do instead.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48976
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the explanation given for using System.exit(0) when you have used JOptionPane. Thank you, Peter Chase, for pointing out it is mistaken advice.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic