Runtime.addShutdownHook()使用

主要用来在JVM关闭前,执行一些自定义操作。

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Runtime.getRuntime().addShutdownHook(t6);
		t1.start();
		t2.start();
		t3.start();
		t4.start();
		t5.start();
		
	}

	private static Thread t1 = new Thread() {
		public void run() {
			System.out.println("thread1...");
		}
	};

	private static Thread t2 = new Thread() {
		public void run() {
			System.out.println("thread2...");
		}
	};
	private static Thread t3 = new Thread() {
		public void run() {
			System.out.println("thread3...");
		}
	};
	private static Thread t4 = new Thread() {
		public void run() {
			System.out.println("thread4...");
		}
	};
	private static Thread t5 = new Thread() {
		public void run() {
			System.out.println("thread5...");
		}
	};

	private static Thread t6 = new Thread() {
		public void run() {
			System.out.println("thread6...");
		}
	};
}

执行结果:

thread2...
thread1...
thread3...
thread4...
thread5...
thread6...
  // thread6总在最后执行


你可能感兴趣的:(Runtime.addShutdownHook()使用)