How To Unopen Coffee Programme Or Swing Application Amongst Example

How To Unopen Coffee Programme Or Swing Application Amongst Example - Hallo sahabat BEST LEARNING JAVA, Pada Artikel yang anda baca kali ini dengan judul How To Unopen Coffee Programme Or Swing Application Amongst Example, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel core java, Artikel Swing, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : How To Unopen Coffee Programme Or Swing Application Amongst Example
link : How To Unopen Coffee Programme Or Swing Application Amongst Example

Baca juga


How To Unopen Coffee Programme Or Swing Application Amongst Example

In gild to close Java program nosotros require to consider which sort of Java application it is?, because outcome of Java application varies betwixt normal marrow coffee program to swing GUI application. In full general all Java programme terminates automatically in 1 lawsuit all user threads created yesteryear programme finishes its execution, including primary thread. JVM doesn't await for daemon thread so equally shortly equally final user thread finished, Java programme volition terminate. If yous desire to unopen or terminate your coffee application earlier this your entirely choice is to piece of job System.exit(int status) or Runtime.getRuntime().exit(). This displace JVM to abandon all threads in addition to leave of absence immediately. Shutdown hooks are instruct called to allow to a greater extent than or less final infinitesimal clearing earlier JVM genuinely terminates. System.exit() equally good convey an int status parameter where a non null value announce abnormal execute in addition to its the lawsuit returned yesteryear coffee ascendance to caller. In this coffee tutorial nosotros volition meet representative of closing both Java programme in addition to Java Swing application. This is equally good a good swing interview questions which yous tin post away inquire to whatever GUI developer in addition to my minute article inward swing later on writing invokeAndWait vs invokeLater



Example of Closing Java programme using System.exit()

Here is a code representative of closing Java programme yesteryear calling System.exit() method. Remember non null declaration to exit() method similar exit(1) denotes abnormal outcome of Java application.


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

/**
 *Java programme which terminates itself yesteryear using System.exit() method , non null telephone telephone 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 leave of absence says abnormal outcome 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 commencement creates a Thread  in main method in addition to start it  which prints “User thread is running” in addition to than primary thread slumber for 200 Milli second, till than other user thread is running in addition to printing exactly in 1 lawsuit main thread woken upwards it terminates the programme yesteryear 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 in addition to DISPOSE_ON_CLOSE is that if yous receive got a non daemon thread running it volition non live on closed inward instance of DISPOSE_ON_CLOSE, piece EXIT_ON_CLOSE terminate JVM fifty-fifty if user thread is running. run the below representative yesteryear un comment DISPOSE_ON_CLOSE inward your IDE in addition to yous tin post away meet user thread running fifty-fifty later on clicking on unopen button. hither is a consummate code representative of closing Swing application inward Java.

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

/**
 * Java swing programme which terminates itself yesteryear calling  EXIT_ON_CLOSE in addition to 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 terminate 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 almost terminating or closing Java program

Few points worth noting regarding unopen or outcome of Java application from programme itself:
1) System.exit() genuinely calls Runtime.getRuntime().exit() method.
2) Non null declaration to exit() denotes abnormal outcome of Java program.
3) Shutdown hooks are executed earlier Java programme genuinely terminates.
4) There are 2 options to unopen Java Swing application 1 is EXIT_ON_CLOSE in addition to other is DISPOSE_ON_CLOSE.
5) DISPOSE_ON_CLOSE doesn't terminate JVM if whatever user thread is running.
6) You tin post away equally good implement window listener to implement your closing machinery yesteryear using System.exit() inward Swing application.

That's all on how to unopen or terminate Java program. nosotros receive got equally good seen representative of closing Swing application inward Java in addition to difference betwixt EXIT_ON_CLOSE in addition to DISPOSE_ON_CLOSE.

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


Demikianlah Artikel How To Unopen Coffee Programme Or Swing Application Amongst Example

Sekianlah artikel How To Unopen Coffee Programme Or Swing Application Amongst Example kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel How To Unopen Coffee Programme Or Swing Application Amongst Example dengan alamat link https://bestlearningjava.blogspot.com/2019/09/how-to-unopen-coffee-programme-or-swing.html

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

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel