java/concurrency
[Concurrency] - shutdown hook 사용하기
짱가쟁이
2010. 6. 29. 16:13
정상적으로 종료되는 JVM 은 가장 먼저 종료 훅을 실행 시킨다.
Runtime.getRuntime().addShutdownHook(..) 에 등록된 실행되지 않은 스레드를 의미함.
Example
Runtime.getRuntime().addShutdownHook(..) 에 등록된 실행되지 않은 스레드를 의미함.
Example
package study.shutdownhook;
public class Launcher {
public static void main(String[] args) {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("종료훅 실행..");
}
});
ShutdownHookThread t = new ShutdownHookThread();
t.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t.interrupt();
}
}
class ShutdownHookThread extends Thread {
public void run() {
try {
while(!Thread.currentThread().isInterrupted()) {
System.out.println("running...");
Thread.sleep(1000);
}
} catch (InterruptedException ignored) {}
}
}
public class Launcher {
public static void main(String[] args) {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("종료훅 실행..");
}
});
ShutdownHookThread t = new ShutdownHookThread();
t.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t.interrupt();
}
}
class ShutdownHookThread extends Thread {
public void run() {
try {
while(!Thread.currentThread().isInterrupted()) {
System.out.println("running...");
Thread.sleep(1000);
}
} catch (InterruptedException ignored) {}
}
}