您的位置:首页 > 其它

<org manual>翻译--3.5.5 域公式和区间公式

2013-05-24 06:12 369 查看
样例:

package thread;

public class CleanThead {

    /**

     * @param args

     */

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        DestroyableImpl di = new DestroyableImpl();

        ShutdownHook sh = new ShutdownHook(di);

        Runtime.getRuntime().addShutdownHook(sh);

       

        Thread t=new Thread(){

            public void run(){

                System.out.println("第二波清理……");

            }

        };

      

        Runtime.getRuntime().addShutdownHook(t);

       

        di.setDaemon(true);

        di.start();

        System.out.println("Main closed !");

    }

}

class ShutdownHook extends Thread {

    private Destroyable desObj;

  

    public ShutdownHook(Destroyable desObj){

        this.desObj = desObj;

            }

  

    @Override

    public void run() {

        if(this.desObj!=null){

            try{

                this.desObj.destroy();

            }

            catch (Exception e) {

            }

        }

    }

}

 

 class DestroyableImpl extends Thread implements Destroyable {

      

      

        public DestroyableImpl(){

             

                }

      

        @Override

        public void run() {

            try {

                Thread.currentThread().sleep(100);

            } catch (InterruptedException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

            System.out.println("业务运行……");

           

        }

        public void destroy(){

            System.out.println("正在清理……");

        }

 

    }

 

  interface Destroyable {

        /**

         * 实现本接口的类需要实现此方法,方法中包含该类的清理代码,

         * 例如关闭数据库连接、关闭Socket连接等

         *

         */

        public void destroy();

    }

参考:http://www.nq3721.cn/commons/product/ProductView.aspx?productCD=28

通过Runtime.getRuntime().addShutdownHook(Thread hook)

方法可以给当前的进程注册一个清理线程,当进程退出的时候,会执行线程中的代码。

1、为了统一清理线程的实现,可以做一个Destroyable接口,实现此接口的类都有一个destroy()方法,里面包含了该类的清理过程,例如关闭数据库连接、关闭Socket连接等。

Destroyable接口的代码:

package net.blogjava.amplifier;

/**

 * 建立此接口的目的是为了实现程序退出时的清理

 * 在Java控制台程序中,用户可以通过关闭命令行窗口或按下Ctrl+C来结束程序的运行

 * 这时候应该保证资源能够被正确释放,例如数据库的连接、Socket的连接应该关闭

 * 实现了本接口的类的实例引用可以由一个ShutdownHook线程来操作

 * 此线程在虚拟机退出时执行,详见Runtime.addShutdownHook()方法

 * @author amplifier

 *

 */

public interface Destroyable {

    /**

     * 实现本接口的类需要实现此方法,方法中包含该类的清理代码,

     * 例如关闭数据库连接、关闭Socket连接等

     *

     */

    void destroy();

}

2、建立一个继承于Thread的类ShutdownHook,里面的一个域是Destroyable实现类的引用,在该类的构造函数中可以传入该Destroyable实现类。

ShutdownHook的代码:

package net.blogjava.amplifier;

public class ShutdownHook extends Thread {

    private Destroyable desObj;

   

    public ShutdownHook(Destroyable desObj){

        this.desObj = desObj;

            }

   

    @Override

    public void run() {

        if(this.desObj!=null){

            try{

                this.desObj.destroy();

            }

            catch (Exception e) {

            }

        }

    }

    /** *//**

     * @param args

     */

    public static void main(String[] args) {

    }

}

3、在main()方法中注册ShutdownHook。

    public static void main(String[] args){

        DestroyableImpl di = new DestroyableImpl();

        ShutdownHook sh = new ShutdownHook(di);

        Runtime.getRuntime().addShutdownHook(sh);

        Runtime.getRuntime().addShutdownHook(new Thread(){

            public void run(){

                System.out.println("正在退出……");

            }

        });

        di.otherMethod();

    }

 

 

 

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: