How To Unopen Coffee Plan Or Swing Application Amongst Example

In enterprise to close Java program nosotros need to consider which variety of Java application it is?, because final result of Java application varies betwixt normal meat coffee program to swing GUI application. In full general all Java plan terminates automatically i time all user threads created past times plan finishes its execution, including principal thread. JVM doesn't await for daemon thread so equally before long equally terminal user thread finished, Java plan volition terminate. If yous desire to unopen or dismiss your coffee application earlier this your entirely selection is to role System.exit(int status) or Runtime.getRuntime().exit(). This drive JVM to abandon all threads together with operate out immediately. Shutdown hooks are larn called to allow unopen to terminal infinitesimal clearing earlier JVM genuinely terminates. System.exit() too convey an int status parameter where a non null value announce abnormal execute together with its the lawsuit returned past times coffee ascendence to caller. In this coffee tutorial nosotros volition run across illustration of closing both Java plan together with Java Swing application. This is too a good swing interview questions which yous tin inquire to whatever GUI developer together with my minute article inward swing later on writing invokeAndWait vs invokeLater



Example of Closing Java plan using System.exit()

Here is a code illustration of closing Java plan past times calling System.exit() method. Remember non null declaration to exit() method similar exit(1) denotes abnormal final result of Java application.


import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *Java plan which terminates itself past times using System.exit() method , non null telephone band to exit() method denotes abnormal termination.
 */

public class JavaCloseExample {
 
    public static void main(String args[]) throws InterruptedException {
   
       Thread t = new Thread(){
            @Override
           public void run(){
               while(true){
                   System.out.println("User thread is running");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(JavaCloseExample.class.getName()).log(Level.SEVERE, null, ex);
                    }
               }
           }
       };
     
       t.start();
       Thread.sleep(200);
       System.out.println("terminating or closing coffee program");
       System.exit(1); //non null value to operate out says abnormal final result of JVM
    }
}

Output:
User thread is running
User thread is running
terminating or closing coffee program
Java Result: 1  //1 is what nosotros passed to exit() method

This Java plan commencement creates a Thread  in main method together with start it  which prints “User thread is running” together with than principal thread slumber for 200 Milli second, till than other user thread is running together with printing merely i time main thread woken upwards it terminates the plan past times calling exit() method of java.lang.System class.

How to unopen Java swing application from program

JVM if terminal displayable window is disposed off. Difference betwixt EXIT_ON_CLOSE together with DISPOSE_ON_CLOSE is that if yous conduct maintain a non daemon thread running it volition non live on closed inward instance of DISPOSE_ON_CLOSE, spell EXIT_ON_CLOSE dismiss JVM fifty-fifty if user thread is running. run the below illustration past times un comment DISPOSE_ON_CLOSE inward your IDE together with yous tin run across user thread running fifty-fifty later on clicking on unopen button. hither is a consummate code illustration of closing Swing application inward Java.

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

/**
 * Java swing plan which terminates itself past times calling  EXIT_ON_CLOSE together with DISPOSE_ON_CLOSE
 */

public class CloseSwingExample {

    public static void main(String args[]) throws InterruptedException {

        JFrame frame = new JFrame("Sample");
        //frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); won't dismiss JVM if user thread running
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setVisible(true);

        Thread t = new Thread() {

            @Override
            public void run() {
                while (true) {
                    System.out.println("User thread is running");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(CloseSwingExample.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        };

        t.start();

    }
}


Important points nearly terminating or closing Java program

Few points worth noting regarding unopen or final result of Java application from plan itself:
1) System.exit() genuinely calls Runtime.getRuntime().exit() method.
2) Non null declaration to exit() denotes abnormal final result of Java program.
3) Shutdown hooks are executed earlier Java plan genuinely terminates.
4) There are ii options to unopen Java Swing application i is EXIT_ON_CLOSE together with other is DISPOSE_ON_CLOSE.
5) DISPOSE_ON_CLOSE doesn't dismiss JVM if whatever user thread is running.
6) You tin too implement window listener to implement your closing machinery past times using System.exit() inward Swing application.

That's all on how to unopen or dismiss Java program. nosotros conduct maintain too seen illustration of closing Swing application inward Java together with difference betwixt EXIT_ON_CLOSE together with DISPOSE_ON_CLOSE.

Further Reading
Java Swing (GUI) Programming: From Beginner to Expert
How to pose conditional break-point inward Java plan using Eclipse IDE

Sumber https://javarevisited.blogspot.com/

0 Response to "How To Unopen Coffee Plan Or Swing Application Amongst Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel