Runtime.getRuntime().addShutdownHook用法

原创转载请注明出处:http://agilestyle.iteye.com/blog/2395890

JDK Introduction 

Code Demo

package org.fool.test;

public class TestShutdownHook {
    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> System.out.println("Thread1..."));
        Thread thread2 = new Thread(() -> System.out.println("Thread2..."));
        Thread thread3 = new Thread(() -> System.out.println("Thread3..."));

        Thread shutdownHookThread1 = new Thread(() -> System.out.println("ShutdownHook Thread1..."));
        Thread shutdownHookThread2 = new Thread(() -> System.out.println("ShutdownHook Thread2..."));
        Thread shutdownHookThread3 = new Thread(() -> System.out.println("ShutdownHook Thread3..."));

        Runtime.getRuntime().addShutdownHook(shutdownHookThread1);
        Runtime.getRuntime().addShutdownHook(shutdownHookThread2);
        Runtime.getRuntime().addShutdownHook(shutdownHookThread3);
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

Console Output

 

猜你喜欢

转载自agilestyle.iteye.com/blog/2395890
今日推荐