Runtime.getRuntime().addShutdownHook usage

Please indicate the source of the original reprint: 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

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326884692&siteId=291194637