java关闭钩子

用户关闭关闭程序,需要做一些善后的清理工作,但问题是,某些用户不会按照推荐的方法关闭应用程序,肯能导致善后工作无法进行。像tomcat调用server的start方法启动容器,然后会逐级调用start。当发出关闭命令是会启动关闭功能,但是关闭可能会有一些意外产生,导致应用程序没有进入到我们制定的关闭方法去。如何解决这个问题呢,使得即使有意外也能正常进入关闭流程。


    好在java提供了一种优雅的方式去解决这种问题。使得关闭的善后处理的代码能执行。java的关闭钩子能确保总是执行,无论用户如何终止应用程序。除非用户kill,这个是个死穴。


    对java而言,虚拟机会对以下几种操作进行关闭:

   (1)系统调用System.exit()方法

   (2)程序最后一个守护线程退出时,应用程序正常退出。

   (3)用户强行中断程序运行,比如ctrl+c等其他方式中断java程序

   幸运的是,虚拟机在执行关闭操作时,会经过一下两个阶段:

    (1)虚拟机启动所有已经注册的关闭钩子,如果有的话。关闭钩子是先前已经通过RunTime类注册的线程,所有的关闭钩子会并发执行,直到完成任务;

    (2)虚拟机根据情况调用所有没有被调用过的终结器(finalizer)。(在这里不讨论)

    关闭钩子的生成:

    1.创建Thread的子类

    2.实现run方法,应用程序关闭时会调用该方法,不需要调用start方法

    3.在应用中实例化关闭钩子类

    4.使用Runtime注册关闭钩子

例子如下:

ShutdownHookDemo

package test;
public class ShutdownHookDemo {

  public void start() {
    System.out.println("Demo");
    ShutdownHook shutdownHook = new ShutdownHook();
    Runtime.getRuntime().addShutdownHook(shutdownHook);
  }

  public static void main(String[] args) {
    ShutdownHookDemo demo = new ShutdownHookDemo();
    demo.start();
    try {
      System.in.read();
    }
    catch(Exception e) {
    }
    System.out.println("Normal exit");
  }
}

class ShutdownHook extends Thread {
  public void run() {
    System.out.println("Shutting down");
  }
}

 

MySwingApp

package ex16.pyrmont.shutdownhook;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

public class MySwingApp extends JFrame {
  JButton exitButton = new JButton();
  JTextArea jTextArea1 = new JTextArea();
  String dir = System.getProperty("user.dir");
  String filename = "temp.txt";

  public MySwingApp() {
    exitButton.setText("Exit");
    exitButton.setBounds(new Rectangle(304, 248, 76, 37));
    exitButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        exitButton_actionPerformed(e);
      }
    });
    this.getContentPane().setLayout(null);
    jTextArea1.setText("Click the Exit button to quit");
    jTextArea1.setBounds(new Rectangle(9, 7, 371, 235));
    this.getContentPane().add(exitButton, null);
    this.getContentPane().add(jTextArea1, null);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setBounds(0,0, 400, 330);
    this.setVisible(true);
    initialize();
  }

  private void initialize() {
    // create a temp file
    File file = new File(dir, filename);
    try {
      System.out.println("Creating temporary file");
      file.createNewFile();
    }
    catch (IOException e) {
      System.out.println("Failed creating temporary file.");
    }
  }

  private void shutdown() {
    // delete the temp file
    File file = new File(dir, filename);
    if (file.exists()) {
      System.out.println("Deleting temporary file.");
      file.delete();
    }
  }

  void exitButton_actionPerformed(ActionEvent e) {
    shutdown();
    System.exit(0);
  }

  public static void main(String[] args) {
    MySwingApp mySwingApp = new MySwingApp();
  }
}

 

MySwingAppWithShutdownHook

package test;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

public class MySwingAppWithShutdownHook extends JFrame {
  JButton exitButton = new JButton();
  JTextArea jTextArea1 = new JTextArea();
  String dir = System.getProperty("user.dir");
  String filename = "temp.txt";

  public MySwingAppWithShutdownHook() {
    exitButton.setText("Exit");
    exitButton.setBounds(new Rectangle(304, 248, 76, 37));
    exitButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        exitButton_actionPerformed(e);
      }
    });
    this.getContentPane().setLayout(null);
    jTextArea1.setText("Click the Exit button to quit");
    jTextArea1.setBounds(new Rectangle(9, 7, 371, 235));
    this.getContentPane().add(exitButton, null);
    this.getContentPane().add(jTextArea1, null);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setBounds(0,0, 400, 330);
    this.setVisible(true);
    initialize();
  }

  private void initialize() {
    // add shutdown hook
    MyShutdownHook shutdownHook = new MyShutdownHook();
    Runtime.getRuntime().addShutdownHook(shutdownHook);

    // create a temp file
    File file = new File(dir, filename);
    try {
      System.out.println("Creating temporary file");
      file.createNewFile();
    }
    catch (IOException e) {
      System.out.println("Failed creating temporary file.");
    }
  }

  private void shutdown() {
    // delete the temp file
    File file = new File(dir, filename);
    if (file.exists()) {
      System.out.println("Deleting temporary file.");
      file.delete();
    }
  }

  void exitButton_actionPerformed(ActionEvent e) {
    shutdown();
    System.exit(0);
  }

  public static void main(String[] args) {
    MySwingAppWithShutdownHook mySwingApp = new MySwingAppWithShutdownHook();
  }

  private class MyShutdownHook extends Thread {
    public void run() {
      shutdown();
    }
  }
}

 

 

转载于:https://my.oschina.net/zhongwenhao/blog/149633

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值