try-with-resources try(...) で宣言されたリソースは、try-catch コード ブロックの終了後に自動的に閉じられます

try(...) で宣言されたリソースは、try-catch コード ブロックの終了後に自動的に閉じられます。

注意点

  • AutoCloseable インターフェースを実装するクラスの場合、try() でクラスのインスタンスを宣言すると、try が終了した後に close メソッドが自動的に呼び出され、このアクションは finally で呼び出されたメソッドよりも早くなります。
  • 例外が発生するかどうかに関係なく、try() 内のインスタンスは close メソッドと呼ばれます。
  • 自動的に閉じられるオブジェクトを複数、try で宣言することができます.オブジェクトの宣言が早いほど、後で閉じられます
// try-with-resources
try(ServletOutputStream outputStream = response.getOutputStream()) {
    /*ServletOutputStream outputStream = response.getOutputStream()*/
    ImageIO.write(image, "jpg", outputStream);
    outputStream.flush();
    /*outputStream.close();*/
} catch (IOException e) {
    log.error("获取流出错:{}", e.getMessage());
}
public interface AutoCloseable {
    /**
     * Closes this resource, relinquishing any underlying resources.
     * This method is invoked automatically on objects managed by the
     * {@code try}-with-resources statement.
     *
     * <p>While this interface method is declared to throw {@code
     * Exception}, implementers are <em>strongly</em> encouraged to
     * declare concrete implementations of the {@code close} method to
     * throw more specific exceptions, or to throw no exception at all
     * if the close operation cannot fail.
     *
     * <p> Cases where the close operation may fail require careful
     * attention by implementers. It is strongly advised to relinquish
     * the underlying resources and to internally <em>mark</em> the
     * resource as closed, prior to throwing the exception. The {@code
     * close} method is unlikely to be invoked more than once and so
     * this ensures that the resources are released in a timely manner.
     * Furthermore it reduces problems that could arise when the resource
     * wraps, or is wrapped, by another resource.
     *
     * <p><em>Implementers of this interface are also strongly advised
     * to not have the {@code close} method throw {@link
     * InterruptedException}.</em>
     *
     * This exception interacts with a thread's interrupted status,
     * and runtime misbehavior is likely to occur if an {@code
     * InterruptedException} is {@linkplain Throwable#addSuppressed
     * suppressed}.
     *
     * More generally, if it would cause problems for an
     * exception to be suppressed, the {@code AutoCloseable.close}
     * method should not throw it.
     *
     * <p>Note that unlike the {@link java.io.Closeable#close close}
     * method of {@link java.io.Closeable}, this {@code close} method
     * is <em>not</em> required to be idempotent.  In other words,
     * calling this {@code close} method more than once may have some
     * visible side effect, unlike {@code Closeable.close} which is
     * required to have no effect if called more than once.
     *
     * However, implementers of this interface are strongly encouraged
     * to make their {@code close} methods idempotent.
     *
     * @throws Exception if this resource cannot be closed
     */
    /*关闭此资源,放弃任何底层资源。在try-with-resources语句管理的对象上自动调用此方法。虽然这    
      个接口方法被声明为抛出Exception,但强烈鼓励实现者声明关闭方法的具体实现来抛出更具体的异    
      常,或者如果关闭操作不能失败,则完全不抛出异常。关闭操作可能失败的情况需要实现者仔细注意。 
      强烈建议在抛出异常之前放弃底层资源,并在内部将资源标记为关闭。close方法不太可能被多次调用, 
      因此这确保了资源被及时释放。此外,它还减少了资源被另一个资源包装或被包装时可能出现的问题。 
      强烈建议此接口的实现者不要使用close方法抛出InterruptedException。此异常与线程的中断状态交 
      互,如果InterruptedException被抑制,则很可能发生运行时不当行为。更一般地说,如果它会导致 
      被抑制的异常出现问题,则使用AutoCloseable。Close方法不应该抛出它。注意,与java.io的关闭方 
      法不同。可封闭的,这个封闭方法不需要幂等。换句话说,与Closeable不同,多次调用这个close方法 
      可能会产生一些可见的副作用。关闭,如果多次调用则要求没有效果。但是,强烈建议这个接口的实现 
      者使他们的close方法幂等。抛出:异常—如果不能关闭此资源*/
    void close() throws Exception;
}

 

Guess you like

Origin blog.csdn.net/ElendaLee/article/details/127486731