Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to turn off the hook for the JAVA virtual machine

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to turn off the hook in the JAVA virtual machine". Many people will encounter this dilemma in the operation of the actual case, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Java programs often encounter processes that hang up, and some states are not saved correctly, so you need to execute some code to clean up the site when JVM is turned off. ShutdownHook in JAVA provides a better solution.

JDK provides a Java.Runtime.addShutdownHook (Thread hook) method to register a hook that JVM closes, which can be called in the following scenarios:

The program exits normally

Use System.exit ()

The terminal uses interrupts triggered by Ctrl+C

System shutdown

OutOfMemory downtime

Use the Kill pid command to kill the process (note: it will not be called when using kill-9 pid)

Here is the definition of hooks in JDK1.7:

Public void addShutdownHook (Thread hook)

Parameters:

Hook-An initialized but unstarted Thread object

Throw:

IllegalArgumentException-If the specified hook has already been registered, or if it can be determined that the hook is already running or has already been run

IllegalStateException-If the virtual machine is already in the process of shutting down

SecurityException-If a security manager is present and it denies RuntimePermission ("shutdownHooks")

Start with the following versions:

1.3

See also:

RemoveShutdownHook (java.lang.Thread), halt (int), exit (int)

First, let's test the first condition in which the program exits normally:

Package com.hook

Import java.util.concurrent.TimeUnit

Public class HookTest

{

Public void start ()

{

Runtime.getRuntime () .addShutdownHook (new Thread (new Runnable () {)

@ Override

Public void run ()

{

System.out.println ("Execute Hook.")

}

}))

}

Public static void main (String [] args)

{

New HookTest () .start ()

System.out.println ("The Application is doing something")

Try

{

TimeUnit.MILLISECONDS.sleep (5000)

}

Catch (InterruptedException e)

{

E.printStackTrace ()

}

}

}

Running result:

The Application is doing something Execute Hook.

As you can see above, the close hook is called when the main thread finishes running.

Let's test the fifth case (the order is a little out of order, and you care about these details):

Package com.hook

Import java.util.concurrent.TimeUnit

Public class HookTest2

{

Public void start ()

{

Runtime.getRuntime () .addShutdownHook (new Thread (new Runnable () {)

@ Override

Public void run ()

{

System.out.println ("Execute Hook.")

}

}))

}

Public static void main (String [] args)

{

New HookTest () .start ()

System.out.println ("The Application is doing something")

Byte [] b = new byte [500,1024,1024]

Try

{

TimeUnit.MILLISECONDS.sleep (5000)

}

Catch (InterruptedException e)

{

E.printStackTrace ()

}

}

}

The running parameter is set to:-Xmx20M to ensure that OutOfMemoryError will occur.

Running result:

The Application is doing something Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at com.hook.HookTest2.main (HookTest2.java:22) Execute Hook.

You can see that the program calls the close hook when it encounters a memory overflow error, unlike in the first case, the program waits for the 5000ms to finish running and launches the call to close the hook.

Next, let's test the third scenario:

Package com.hook

Import java.util.concurrent.TimeUnit

Public class HookTest3

{

Public void start ()

{

Runtime.getRuntime () .addShutdownHook (new Thread (new Runnable () {)

@ Override

Public void run ()

{

System.out.println ("Execute Hook.")

}

}))

}

Public static void main (String [] args)

{

New HookTest3 () .start ()

Thread thread = new Thread (new Runnable () {

@ Override

Public void run ()

{

While (true)

{

System.out.println ("thread is running....")

Try

{

TimeUnit.MILLISECONDS.sleep (100)

}

Catch (InterruptedException e)

{

E.printStackTrace ()

}

}

}

});

Thread.start ()

}

}

Compile: javac com/hook/HookTest3.java from the command line

Run on the command line: java com.hook.HookTest3

Running result:

You can see that the effect is as expected.

This is the end of "how to turn off the hook on the JAVA virtual machine". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report