Click here to watch in Youtube :
https://www.youtube.com/watch?v=HpmerZsuthM&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
Java Tutorial: Java Runtime class (shutdown hook_V1) |
Java Tutorial: Java Runtime class (shutdown hook_V1) |
public class ShudownHookThread extends Thread { public void run() { System.out.println("Shut down hook task is completed.."); } }ShutdownHookDemo.java
/* * public void addShutdownHook(Thread hook) * * Parameters: * ----------- * * hook - An initialized but unstarted Thread object */ public class ShutdownHookDemo { public static void main(String[] args) { /* * Returns the runtime object associated with the * current Java application. */ Runtime runtime = Runtime.getRuntime(); /* * Registers a new virtual-machine shutdown hook. */ runtime.addShutdownHook(new ShudownHookThread()); System.out.println("Now main thread sleeping... press ctrl+c to exit"); try { Thread.sleep(5000); } catch (Exception e) { e.printStackTrace(); } } }Output
Now main thread sleeping... press ctrl+c to exit Shut down hook task is completed..AnnonymousShutdownHook.java
public class AnnonymousShutdownHook { public static void main(String[] args) { Runtime runtime = Runtime.getRuntime(); runtime.addShutdownHook(new Thread() { public void run() { System.out.println("Shut down hook task is completed.."); } }); System.out.println("Now main thread sleeping... press ctrl+c to exit"); try { Thread.sleep(5000); } catch (Exception e) { e.printStackTrace(); } } }Output
Now main thread sleeping... press ctrl+c to exit Shut down hook task is completed.Refer:
https://docs.oracle.com/javase/8/docs/api/index.html?java/lang/Runtime.html
https://sites.google.com/site/ramj2eev1/home/javabasics/ThreadDemo_ShutdownHook_V1_App.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ThreadDemo_ShutdownHook_V1_App
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/c72ef0bb7cf33f5c158c45d824df234766210e9a/BasicJava/ThreadDemo_ShutdownHook_V1_App/?at=master
See also:
No comments:
Post a Comment