Friday 27 January 2017

Java Tutorial: Java Runtime class (shutdown hook_V1)


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) 
ShudownHookThread.java
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

Click the below link to download the code:
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:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment