addShutdownHook

  • 忘れそうなのでメモ
public class Test {
	public static void main(String[] args) throws Throwable {
		new Test().execute();
	}
	public Test() {
		init();
	}
	private void init() {
		Runtime.getRuntime().addShutdownHook(
			new Thread() {
				public void run() {
					System.out.print("addShutdownHook");
				}
			}
		);
	}
	private static final int WAIT_TIME = 10;
	public void execute() throws Throwable {
		System.out.println("start");
		System.out.println("wait " + WAIT_TIME + " times...");
		for (int idx=0; idx<WAIT_TIME; idx++) {
			Thread.sleep(1000);
			System.out.print('.');
		}
		System.out.println("complete");
	}
}
  • ポイントは↓。
Runtime.getRuntime().addShutdownHook(
	new Thread() {
		public void run() {
			System.out.print("addShutdownHook");
		}
	}
);