-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShutdownHookExample.java
More file actions
29 lines (22 loc) · 641 Bytes
/
ShutdownHookExample.java
File metadata and controls
29 lines (22 loc) · 641 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package mthread;
public class ShutdownHookExample {
public static void main(String[] args) {
Thread t1 = new Thread() {
@Override
public void run() {
System.out.println("Shut down hook run this method. ");
}
};
Runtime r = Runtime.getRuntime();
r.addShutdownHook(t1);
try {
System.out.println("Press ctrl+c in command window to exit program.");
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Calling System.exit()...");
System.exit(0); // exit program in the middle
System.out.println("After 3 second..");
}
}