How To Unopen Coffee Programme Or Swing Application Alongside Example

In social club to close Java program nosotros ask to consider which form of Java application it is?, because final result of Java application varies betwixt normal gist coffee program to swing GUI application. In full general all Java programme terminates automatically 1 time all user threads created past times programme finishes its execution, including principal thread. JVM doesn't facial expression for daemon thread so every bit presently every bit final user thread finished, Java programme volition terminate. If you lot desire to unopen or move your coffee application earlier this your alone choice is to role System.exit(int status) or Runtime.getRuntime().exit(). This movement JVM to abandon all threads together with operate out immediately. Shutdown hooks are larn called to allow to a greater extent than or less final infinitesimal clearing earlier JVM truly terminates. System.exit() likewise convey an int status parameter where a non null value announce abnormal execute together with its the effect returned past times coffee ascendency to caller. In this coffee tutorial nosotros volition come across illustration of closing both Java programme together with Java Swing application. This is likewise a good swing interview questions which you lot tin inquire to whatsoever GUI developer together with my minute article inwards swing afterward writing invokeAndWait vs invokeLater



Example of Closing Java programme using System.exit()

Here is a code illustration of closing Java programme 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 programme which terminates itself past times using System.exit() method , non null telephone phone 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 programme start 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 exactly 1 time main thread woken upwards it terminates the programme past times calling exit() method of java.lang.System class.

How to unopen Java swing application from program

JVM if final displayable window is disposed off. Difference betwixt EXIT_ON_CLOSE together with DISPOSE_ON_CLOSE is that if you lot guide keep a non daemon thread running it volition non locomote closed inwards illustration of DISPOSE_ON_CLOSE, piece EXIT_ON_CLOSE move JVM fifty-fifty if user thread is running. run the below illustration past times un comment DISPOSE_ON_CLOSE inwards your IDE together with you lot tin come across user thread running fifty-fifty afterward clicking on unopen button. hither is a consummate code illustration of closing Swing application inwards Java.

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

/**
 * Java swing programme 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 move 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 nigh terminating or closing Java program

Few points worth noting regarding unopen or final result of Java application from programme itself:
1) System.exit() truly 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 programme truly terminates.
4) There are 2 options to unopen Java Swing application 1 is EXIT_ON_CLOSE together with other is DISPOSE_ON_CLOSE.
5) DISPOSE_ON_CLOSE doesn't move JVM if whatsoever user thread is running.
6) You tin likewise implement window listener to implement your closing machinery past times using System.exit() inwards Swing application.

That's all on how to unopen or move Java program. nosotros guide keep likewise seen illustration of closing Swing application inwards 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 inwards Java programme using Eclipse IDE

Belum ada Komentar untuk "How To Unopen Coffee Programme Or Swing Application Alongside Example"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel