---
title: "Runtime.getRuntime().addShutdownHook用法"
author: "fairy_xzc"
date: "2017-10-11T16:11:00Z"
tags: []
categories: ["编程语言"]
slug: "runtime-getruntime-addshutdownhook"
draft: false
lastmod: "2017-10-11T16:18:51Z"
id: 2395890
url: "https://agilestyle.iteye.com/blog/2395890"
---

**原创转载请注明出处：[http://agilestyle.iteye.com/blog/2395890](/blog/2395890)**

 

## JDK Introduction![](http://dl2.iteye.com/upload/attachment/0127/3225/6c6a6c67-8fa9-31e1-9a7e-b4b1f6157470.png)

## 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

![](http://dl2.iteye.com/upload/attachment/0127/3227/1aa7ed1f-cb18-3686-a9ca-fa93b1c40206.png)