[code]
// The Shutdown class is a sample class to illustrate the
// use of the addShutdownHook method
class Shutdown {
private Thread thread = null;
public Shutdown() {
thread = new Thread("Sample thread") {
public void run() {
while (true) {
System.out.println("[Sample thread] Sample thread speaking...");
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException ie) {
break;
}
}
System.out.println("[Sample thread] Stopped");
}
};
thread.start();
}
public void stopThread() {
thread.interrupt();
}
}
// The ShutdownThread is the thread we pass to the
// addShutdownHook method
class ShutdownThread extends Thread {
private Shutdown shutdown = null;
public ShutdownThread(Shutdown shutdown) {
super();
this.shutdown = shutdown;
}
public void run() {
System.out.println("[Shutdown thread] Shutting down");
shutdown.stopThread();
System.out.println("[Shutdown thread] Shutdown complete");
}
}
// And finally a Main class which tests the two classes
// We let the sample thread run for 10 seconds and then
// force a Shutdown with System.exit(0). You may stop the
// program early by pressing CTRL-C.
public class Main {
public static void main(String [] args) {
Shutdown shutdown = new Shutdown();
try {
Runtime.getRuntime().addShutdownHook(new ShutdownThread(shutdown));
System.out.println("[Main thread] Shutdown hook added");
} catch (Throwable t) {
// we get here when the program is run with java
// version 1.2.2 or older
System.out.println("[Main thread] Could not add Shutdown hook");
}
try {
Thread.currentThread().sleep(10000);
} catch (InterruptedException ie) {
}
System.exit(0);
}
}
[/code]
ref: http://www.esus.com/docs/GetQuestionPage.jsp?uid=392
// The Shutdown class is a sample class to illustrate the
// use of the addShutdownHook method
class Shutdown {
private Thread thread = null;
public Shutdown() {
thread = new Thread("Sample thread") {
public void run() {
while (true) {
System.out.println("[Sample thread] Sample thread speaking...");
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException ie) {
break;
}
}
System.out.println("[Sample thread] Stopped");
}
};
thread.start();
}
public void stopThread() {
thread.interrupt();
}
}
// The ShutdownThread is the thread we pass to the
// addShutdownHook method
class ShutdownThread extends Thread {
private Shutdown shutdown = null;
public ShutdownThread(Shutdown shutdown) {
super();
this.shutdown = shutdown;
}
public void run() {
System.out.println("[Shutdown thread] Shutting down");
shutdown.stopThread();
System.out.println("[Shutdown thread] Shutdown complete");
}
}
// And finally a Main class which tests the two classes
// We let the sample thread run for 10 seconds and then
// force a Shutdown with System.exit(0). You may stop the
// program early by pressing CTRL-C.
public class Main {
public static void main(String [] args) {
Shutdown shutdown = new Shutdown();
try {
Runtime.getRuntime().addShutdownHook(new ShutdownThread(shutdown));
System.out.println("[Main thread] Shutdown hook added");
} catch (Throwable t) {
// we get here when the program is run with java
// version 1.2.2 or older
System.out.println("[Main thread] Could not add Shutdown hook");
}
try {
Thread.currentThread().sleep(10000);
} catch (InterruptedException ie) {
}
System.exit(0);
}
}
[/code]
ref: http://www.esus.com/docs/GetQuestionPage.jsp?uid=392
本文通过一个示例展示了如何在Java中使用addShutdownHook方法来优雅地关闭应用程序。示例中创建了一个名为Shutdown的类,该类启动了一个持续运行的线程并在接收到停止信号时终止。另外还定义了一个ShutdownThread类作为关闭钩子,用于在系统关闭时调用Shutdown实例的stopThread方法以中断活动线程。

被折叠的 条评论
为什么被折叠?



