Interface Autocloseable is a new interface jdk1.7 appears in the Java.lang package, which can be used in conjunction with the Try-with-resources new syntax feature that appears in jdk1.7 to automatically shut down a system resource such as file, network, etc. The interface defines only one method:
void Close () throws Exception
Use the following code specifically:
Package Com.xxm.java.io;public class Autocloseablestudy {public static void main (string[] args) {Try (MyResource resource = new MyResource ()) {resource.test ();}}} Class MyResource implements autocloseable{@Overridepublic void Close () {System.out.println ("closed");}}
Defines a MyResource class that describes a system resource, implements the Close method in the Autocloseable interface, and uses the try-with-resources in the Main method The code initializes the resource and carries out the related business logic operations, where the code for the initialization of the resource is generally defined in parentheses, and the business logic operation code for that resource is generally written in curly braces. The release of the resource will automatically call the Close method after the execution of the try-with-resources after execution of the Resource.test () is finished. The benefits of doing so personally think there are a few things:
1, simplify the code, to prevent the loss of resources caused by the failure of the resource waste and other exceptions, the following is the code format we used to write:
public void Test () {MyResource resource = Null;try{resource = new MyResource (); Resource.test ();} Finally{resource.close ();}}
2, to prevent the exception of the business code is suppressed, such as the code, the try code block may throw an exception, and finally code block may throw an exception, when two exceptions are both thrown, the try code block throws the exception will be suppressed off, So the exception that is often caught is not what we want, and the exception in the Try-with-resources code block can be thrown normally, instead, the exception thrown by the Close method is suppressed off.
Today, I looked at the API documentation for JAVA9 in detail, and there are a few points I think are the better points to be written here:
1, we usually write a base class when possible to implement the Autocloseable interface, perhaps not after each sub-class will use the autocloseable features.
2, non-blocking IO (NIO) is not necessary to re-try-with-resources the code block.
3. When we rewrite the Close method, we try to subdivide the possible exceptions (do not write throws Exception).
4. If there is an exception in close, simply handle the condition of the resource and identify the state of the resource before throwing the exception.
5, Close method is best not InterruptedException
to throwException,因为
InterruptedException
exceptions can be suppressed and may lead to unintended consequences.
6, unlike the Close method in Closeable, the Close method in autocloseable does not require idempotent, and multiple invocations may have different results, but it is recommended to implement the Close method to idempotent in the implementation class.
If some of the wrong places, welcome criticism correct!
This article is from the "I am it male" blog, please be sure to keep this source http://sammy.blog.51cto.com/2038422/1975869
Talking about the Autocloseable interface in Java