Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
May 7, 1999

Java Virtual Machine Doesn't Exit

You may have been surprised and frustrated to find that in some cases, the Java Virtual Machine (JVM) does not stop running when your application exits the static main() method. For example:
 
public class FrameTest extends java.awt.Frame {
	public static void main(String[] args) {
		FrameTest ft = new FrameTest();
		ft.setVisible(true);
		ft.setVisible(false);
		ft.dispose();
		ft = null;
	}  //  public static void main()
}  //  public class FrameTest extends java.awt.Frame
When this code is run as an application, the JVM will not exit. However, it will exit if you delete the calls to setVisible() and run it again. The reason for this change in behavior is that making the Frame visible activates the event thread, which is responsible for calling event handler methods and for painting. Once the event thread is running, the JVM doesn't exit because the event thread is not a daemon thread, and the JVM only exits when no daemon threads are running. To force the JVM to exit, you should call the System.exit() method, which takes a single integer value as a parameter. For example:
 
public class FrameTest extends java.awt.Frame {
	public static void main(String[] args) {
		FrameTest ft = new FrameTest();
		ft.setVisible(true);
		ft.setVisible(false);
		ft.dispose();
		ft = null;
		System.exit(0);
	}  //  public static void main()
}  //  public class FrameTest extends java.awt.Frame
Brett Spell
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap