Java Close Hook

Source: Internet
Author: User


The user closes the shutdown program and needs to do some cleanup work, but the problem is that some users will not close the application in the recommended way, and will be able to cause the aftermath to fail. The Start method, like the Tomcat call server, starts the container and then calls the start step by step. When the shutdown command is issued, the shutdown feature is turned on, but the shutdown may have some unintended impact, resulting in the application not going to the shutdown method we developed. How to solve this problem, so that even if there are accidents can be normal access to the shutdown process.


Fortunately, Java provides an elegant way to solve this problem. Enable the shutdown of the aftermath of the code can be executed. The Java close Hook ensures that it is always executed, regardless of how the user terminates the application. Unless the user kills, this is a dead hole.


for Java, a virtual opportunity closes the following operations:

(1) system call System.exit () method

(2) when the last daemon of the program exits, the application exits normally.

(3) The user forcibly interrupts the program, such as CTRL + C and other ways to interrupt the Java program


turn off the hook's build:

1. Create a subclass of thread

2. Implement the Run method, which is invoked when the application shuts down, and does not need to invoke the Start method

3. Instantiate close hook in application

4. Use runtime registration to close hooks

public class Exithook
{public
	static void Main (string[] args)	
		throws IOException
	{
		final FileOutputStream Fos;
		FOS = new FileOutputStream ("A.bin");
		SYSTEM.OUT.PRINTLN (The program opens the physical resource.) ");
		Close Hook
		runtime.getruntime (). Addshutdownhook for System registration (
			new Thread () 
			{public
				void run ()
				{
					//Use close hook to close resource
					if (FOS!= null)
					{
						try
						{
							fos.close ()
						}
						catch (Exception ex)
						{
							ex.printstacktrace ();
						}
					}
					SYSTEM.OUT.PRINTLN (the program shuts down the physical resource.) ");
				}
			});
		System.exit (0);
	}
}

close the hook to reclaim system resources when the program exits, and run the above program to see


The system can shut down the physical resources normally.

Recommendation: The same JVM is best to use only a close hook, instead of each service using a different close hook, using multiple close hooks may appear that the current hook depends on the service may have been another close hook closed. To avoid this situation, it is recommended that the shutdown operation be serially executed in a single thread, thus avoiding problems such as a race condition or deadlock between the shutdown operation.


http://blog.csdn.net/huangmin2050/article/details/7694610
Http://blog.sina.com.cn/s/blog_6145ed8101015tbu.html
Http://www.cnblogs.com/langtianya/p/4300282.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.