java.lang.AutoCloseable


/**
 * 保持資源(文件或者socket)直到它關閉的類.{@code AutoCloseable} 的 {@link #close()}方法
 * 在退出 {@code try}-with-resources 塊時自動被調用.指令應該保證資源被立即釋放,
 * 避免資源超出限制導致錯誤.
 * 
 * @apiNote
 * <p>實際上,並不需要子類都實現自動釋放資源,由基類來實現釋放資源也是可行的,通常推薦使用
 * {@code try}-with-resources 塊處理資源.然而,因為{@link java.util.stream.Stream}
 * 支持基於I/O和非I/O倆種形式,因此{@code try}-with-resources 塊通常在非I/O形式時沒有必要使用.
 * @since 1.7
 */
/**
 * An object that may hold resources (such as file or socket handles)
 * until it is closed. The {@link #close()} method of an {@code AutoCloseable}
 * object is called automatically when exiting a {@code
 * try}-with-resources block for which the object has been declared in
 * the resource specification header. This construction ensures prompt
 * release, avoiding resource exhaustion exceptions and errors that
 * may otherwise occur.
 *
 * @apiNote
 * <p>It is possible, and in fact common, for a base class to
 * implement AutoCloseable even though not all of its subclasses or
 * instances will hold releasable resources.  For code that must operate
 * in complete generality, or when it is known that the {@code AutoCloseable}
 * instance requires resource release, it is recommended to use {@code
 * try}-with-resources constructions. However, when using facilities such as
 * {@link java.util.stream.Stream} that support both I/O-based and
 * non-I/O-based forms, {@code try}-with-resources blocks are in
 * general unnecessary when using non-I/O-based forms.
 *
 * @author Josh Bloch
 * @since 1.7
 */
public interface AutoCloseable {
    /**
     * 關閉資源,放棄任何底層資源.在 {@code try}-with-resources中此方法被自動使用.
     * <p> 此方法被聲明成拋出{@code Exception},強烈建議拋出自己專用異常,
     * 或者如果操縱不允許失敗,不拋出異常.
     * 
     * <p> 實現的closeclose會失敗時需要特別小心,強烈建議放棄底層資源並且將內部資源標記為已經closed,
     * 然后再拋出異常.{@code close}方法調用次數不會超過一次,所以確保資源能夠
     * 及時釋放.此外,它還減少了資源包裹容易出現的問題.
     * 
     * <p> 強烈建議實現類不要拋出{@link InterruptedException}
     */
    /**
     * 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>
     *
     * 此異常與線程狀態交互,如果{@linkplain Throwable#addSuppressed 禁止}
     * @{code InterruptedException}可能會導致非預期運行時行為
     *
     * 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}.
     * 
     * 更普遍來說,如果異常應該被禁止,{@code AutoCloseable.close}就不應該拋出它.
     *
     * More generally, if it would cause problems for an
     * exception to be suppressed, the {@code AutoCloseable.close}
     * method should not throw it.
     * 
     * 注意:與{@link java.io.Closeable#close close}不同,{@code close}不要求是冪等.
     * 換句話來說,調用此方法超過一次可能會導致副作用.而{@code Closeable.close}可以多次調用.
     * 
     * 但是,強烈將此方法實現成冪等操作.
     *
     * <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
     */
    void close() throws Exception;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM