ホームページ  >  記事  >  Java  >  Java の AutoCloseable インターフェイス

Java の AutoCloseable インターフェイス

coldplay.xixi
coldplay.xixi転載
2020-11-02 17:45:582553ブラウズ

今日のJava基本チュートリアルのコラムでは、AutoCloseableインターフェースを紹介します。

Java の AutoCloseable インターフェイス

この記事では、try-with-resources 構文のより詳細な分析を実施し、それが糖衣構文の一種であることを検証し、実際の実装に関するフィードバックを提供します。コンパイル結果、この記事を読んだ後、AutoCloseable の使用法について新たな洞察が得られると思います。

1. はじめに

私は最近、JDK 7 の新機能である try-with-resources 構文を使用しました。詳細は次のとおりです。私の学習結果をみんなと共有してください。

2. 理解して使用するだけです

try-with-resources 構文は比較的簡単に使用でき、通常はサンプル コードを検索するだけで使用できます。 JDK によるこの構文のサポートは、リソースの管理を改善すること、より正確にはリソースを解放することです。

リソース クラスがこのインターフェイスの close メソッドを実装する場合、try-with-resources 構文を使用して作成されたリソースが例外をスローした後、例外がない場合、JVM は自動的に close メソッドを呼び出してリソースを解放します。コード ブロックを試行するときに、close メソッドも自動的に呼び出されます。データベース接続クラス Connection と同様、io クラスの InputStream または OutputStream はすべて、このインターフェイスを直接的または間接的に実装します。

コード例を通じてその使用方法を学びましょう。まず、AutoCloseable インターフェイスを実装するクラスを作成します:

public class Resource implements AutoCloseable{
    public void read() {
        System.out.println("do something");
    }

    @Override
    public void close() throws Exception {
        System.out.println("closed");
    }
}复制代码

1。この構文を使用しない場合は、アクティブに ## を閉じる必要があります。 #
public static void f1(){
    Resource resource = new Resource();
    try {
        resource.read();
    } finally{
        try {
            resource.close();
        } catch (Exception e) {
        }
    }
}复制代码

2. この構文を使用すると、コードはよりエレガントで簡潔になります

public static void f2(){
    try(Resource resource = new Resource()) {
        resource.read();
    } catch (Exception e) {
    }
}复制代码

注: read メソッド自体は例外をスローしませんが、コーディング時に catch ブロックが必要であることが示されています。 , したがって、この例外のキャプチャは、キャプチャされた close メソッド例外によってスローされたことがわかります。

3. Closeable インターフェイスに変更するか、

次に、Resource クラスの AutoCloseable インターフェイスを Closeable に変更します (以下に示すように)。このとき、例外を変更する必要があります。 close メソッドのシグネチャを IOException に設定しないと、コンパイルは失敗します。次のコードは、例外シグネチャを満たすために積極的に IOException をスローしていることに注意してください。そうでない場合は、例外シグネチャを削除して、例外シグネチャなしに変更するように求めるプロンプトが表示されます。したがって、Closeable インターフェイスを実装した後は、例外シグネチャが存在しないか、IOException またはそのサブクラスになります。

public class Resource implements Closeable{
    public void read() {
        System.out.println("do something");
    }

    @Override
    public void close() throws IOException{
        System.out.println("closed");
        throw new IOException();
    }
}复制代码
次に、Closeable と AutoCloseable の関係を調べます。継承関係に加えて、Closeable は close メソッドの例外シグネチャを Exception から IOException までわずかに具体的にするだけであることがわかります。

public interface AutoCloseable {
    //省略Java doc
    void close() throws Exception;
}复制代码
public interface Closeable extends AutoCloseable {
    ////省略Java doc
    public void close() throws IOException;
}复制代码
したがって、JDK に java.lang.AutoCloseable インターフェイスを実装する場合でも、java.io.Closeable インターフェイスを実装する場合でも、try-with-resources 構文を使用できます。ここでのもう 1 つの違いは、2 つのインターフェイスのパケット パスの違いであることに注意してください。

3. Java ドキュメントの読み取り

1. AutoCloseable の Java ドキュメント

閉じられるまでリソース (ファイルやソケット ハンドルなど) を保持する可能性のあるオブジェクト. AutoCloseable オブジェクトの close() メソッドは、オブジェクトがリソース仕様ヘッダーで宣言されている try-with-resources ブロックを終了するときに自動的に呼び出されます。この構造により、迅速な解放が確保され、リソース枯渇の例外やエラーが回避されます。

すべてのサブクラスやインスタンスが解放可能なリソースを保持しているわけではない場合でも、基本クラスが AutoCloseable を実装することは可能であり、実際に一般的です。完全な汎用性で動作する必要があるコードの場合、またはAutoCloseable インスタンスにはリソースの解放が必要であることが知られているため、try-with-resources 構造を使用することをお勧めしますが、I/O ベースと非 I/O の両方をサポートする java.util.stream.Stream などの機能を使用する場合は、 -ベースのフォーム、try-with-resources ブロックは、非 I/O ベースのフォームを使用する場合、一般に不要です。

#ファイルやソケットなどのオブジェクトは、close メソッドを呼び出した後、保持しているリソースを解放します。 try-with-resources 構文を使用して AutoCloseable インターフェイスを実装するクラスのオブジェクトをインスタンス化する場合、 close() メソッドが自動的に呼び出され、リソースがタイムリーに解放され、リソース枯渇の問題が回避されます。

一部の基本クラスが AutoCloseable インターフェイスを実装しているのをよく見かけますが、これは、すべてのサブクラスがリソースを解放する必要がない場合や、すべてのインスタンスが解放する必要のあるリソースを保持しているわけではない場合でも、実現可能です。操作を一般的な方法で終了する必要がある場合、またはそのインスタンスがリソースを解放する必要があることがわかっている場合は、AutoCloseable インターフェイスを実装し、try-with-resources 構文を使用することをお勧めします。

次に、AutoCloseable.close メソッドに関する Java ドキュメントを見てみましょう。以下は原文です:

このリソースを閉じ、基礎となるリソースをすべて放棄します。このメソッドは、try-with-resources ステートメントによって管理されるオブジェクトに対して自動的に呼び出されます。

While this interface method is declared to throw Exception, implementers are strongly encouraged to declare concrete implementations of the close method to throw more specific exceptions, or to throw no exception at all if the close operation cannot fail.

Cases where the close operation may fail require careful attention by implementers. It is strongly advised to relinquish the underlying resources and to internally mark the resource as closed, prior to throwing the exception. The 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.

Implementers of this interface are also strongly advised to not have the close method throw InterruptedException.

This exception interacts with a thread's interrupted status, and runtime misbehavior is likely to occur if an InterruptedException is suppressed.

More generally, if it would cause problems for an exception to be suppressed, the AutoCloseable.close method should not throw it.

Note that unlike the java.io.Closeable#close close method of java.io.Closeable, this close method is not required to be idempotent. In other words, calling this close method more than once may have some visible side effect, unlike 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 close methods idempotent.

翻译如下:

此方法用于关闭资源,放弃任何底层基础资源。当使用try-with-resources语法管理对象时,close方法将在try代码块逻辑结束后自动被调用。

虽然AutoCloseable中的close方法被声明为抛出Exception这个异常,但是强烈建议实现类声明更加具体的异常,或者当close操作不允许失败时声明为不抛出任何异常。

实现者需要注意close方法操作失败的情况,强烈建议放弃底层资源,并在抛出异常前在内部将资源标注为不可用。close方法不太可能被反复调用,因此这样确保close被调用后资源被及时标志为释放。此外,这样还能减少资源被其他资源包装时可能出现的问题。

强烈建议实现者在实现close方法时不要抛出InterruptedException。InterruptedException与线程状态交互影响,如果处理不当,可能导致运行时错误。更一般的情况下,当一个异常的不当处理会导致题,AutoCloseable.close方法就不应该抛出这个异常。

与java.io.Closeable.close方法不同的是,AutoCloseable.close方法的调用不要求幂等。换句话说,多次调用AutoCloseable.close可能会产生一些可见的副作用,不像Closeable.close允许多次调用。然而,强烈建议实现者将close方法实现为幂等。

2、 Closeable 中的 Java doc

Closeable类上的Java doc无额外有用信息,我们看下Closeable.close方法上的Java doc:

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable#close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

翻译如下:

此方法用于关闭流并释放与之相关的任何系统资源。

如果流已经关闭,则close方法的调用将无任何效果。如同AutoCloseable#close()的Java doc描述的那样,需要关注关闭失败的情况。

在抛出IOException异常前,强烈建议放弃底层资源并在内部标注资源已关闭。

四、揭开魔术面纱

1、怎么实现的

由于不确定try-with-resources是一种语法糖,还是在JVM虚拟机层面新加指令进行的支持,我尝试使用javap解析class文件后,通过解析结果查表对照发现,两段代码不同之处对应的字节码指令并不是此语法的含义,判定其确实是一种语法糖,在变成class文件的时候已经由编译器(javac)处理了。既然如此,我们就直接将其反编译,得到的结果如下:

public static void f1() {
    Resource resource = new Resource();
    try {
        resource.read();
    } finally {
        try {
            resource.close();
        } catch (Exception var7) {
        }
    }
}复制代码
public static void f2() {
    try {
        Resource resource = new Resource();
        Throwable var1 = null;
        try {
            resource.read();
        } catch (Throwable var11) {
            var1 = var11;
            throw var11;
        } finally {
            if (resource != null) {
                if (var1 != null) {
                    try {
                        resource.close();
                    } catch (Throwable var10) {
                        var1.addSuppressed(var10);
                    }
                } else {
                    resource.close();
                }
            }
        }
    } catch (Exception var13) {
    }
}复制代码

可以看到代码片段1与原代码几乎一样,但是代码片段2与原代码确实大相径庭,接着我们来分析下:

**关注点1:**new Resource()操作被放到了try内部了,如果不用try-with-resources语法,我们一般放到try外面。

**关注点2:**resource.read()操作被捕获了Throwable,并通过赋值记录了下来。

**关注点3:**fianally中的if (resource != null)这一步骤是多余的,因为只有在new Resource()操作抛异常才会存在resource为空的情况,然而这个时候就不会执行到这里来了。

**关注点4:**此时一定要执行resource.close()了,当var1不为空(即resource.read()抛出了异常),则需捕获close可能抛出的异常,并调用var1.addSuppressed记录关联try中抛出的异常,我们后面再分析这步骤。

**关注点5:**由于在原始代码里我们捕获了close方法抛出的异常,因此这里当上一步的var1为空时可能抛出的异常需要在最外层捕获。

2、Throwable.addSuppressed

public final synchronized void addSuppressed(Throwable exception) {
    if (exception == this)
        throw new IllegalArgumentException(SELF_SUPPRESSION_MESSAGE, exception);

    if (exception == null)
        throw new NullPointerException(NULL_CAUSE_MESSAGE);

    if (suppressedExceptions == null) // Suppressed exceptions not recorded
        return;

    if (suppressedExceptions == SUPPRESSED_SENTINEL)
        suppressedExceptions = new ArrayList<>(1);

    suppressedExceptions.add(exception);
}复制代码

这段代码我们只需要了解,每一个Throwable实例中有一个List类型的字段,本方法将入参中的异常添加到了这个List的末尾。

Appends the specified exception to the exceptions that were suppressed in order to deliver this exception. This method is thread-safe and typically called (automatically and implicitly) by the try-with-resources statement.

The suppression behavior is enabled unless disabled Throwable(String, Throwable, boolean, boolean) via a constructor. When suppression is disabled, this method does nothing other than to validate its argument.

Note that when one exception causes another exception, the first exception is usually caught and then the second exception is thrown in response. In other words, there is a causal connection between the two exceptions. In contrast, there are situations where two independent exceptions can be thrown in sibling code blocks, in particular in the try block of a try-with-resources statement and the compiler-generated finally block which closes the resource. In these situations, only one of the thrown exceptions can be propagated. In the try-with-resources statement, when there are two such exceptions, the exception originating from the try block is propagated and the exception from the finally block is added to the list of exceptions suppressed by the exception from the try block. As an exception unwinds the stack, it can accumulate multiple suppressed exceptions.

An exception may have suppressed exceptions while also being caused by another exception. Whether or not an exception has a cause is semantically known at the time of its creation, unlike whether or not an exception will suppress other exceptions which is typically only determined after an exception is thrown.

Note that programmer written code is also able to take advantage of calling this method in situations where there are multiple sibling exceptions and only one can be propagated.

翻译如下:

为了传递此异常(入参),将其附加到记录到this中的实例字段(List)中。这个方法是线程安全的,通常由try-with-resources语句(自动和隐式地)调用。除非通过Throwable(String, Throwable, boolean, boolean)构造方法来显示禁用,否则将启用这个行为。禁用后,此方法除了验证参数外不做其他任何事情。

注意,当一个异常引发另一个异常时,通常会捕获第一个异常,然后在响应中抛出第二个异常(就是抛出最上层的异常)。换句话说,这两个异常之间存在因果关系。当然也存在例外情况,可以在兄弟代码块中抛出两个独立的异常,特别是在try-with-resources语句的try块和编译器生成的finally块(用于关闭资源)中。在这些情况下,只能传播一个抛出的异常。在try-with-resources语句中,如果存在两个这样的异常,则传播源自try块的异常,并将来自finally块的异常记录到try代码块抛出的异常中。当你展开一个异常堆栈时,你可以看到它累积的多个记录的未抛出的异常(因为只能抛出一个异常,其他异常只能通过这个方式来记录)。

一个异常可能记录有未抛出的异常,同时其自身也是由另一个异常引起的。一个异常是否由其他异常引起在创建时就已经在语义上知道了(即通过构造函数),这与这个异常是否通过Throwable.addSuppressed方式记录有异常不同,后者通常只在抛出异常后才能知道。

注意,程序员编写的代码,也能够在有多个兄弟异常(兄弟异常可以理解为相互并无关联)且只能传播一个异常的情况下,利用调用此方法的优势。

阅读完这个Java doc注释后,建议结合Throwable类源码加深理解。

五、JDK 9中的改进

通过搜索学习,还了解到在 JDK 9中对此语法进行了改进,JSR334对其进行了描述,感兴趣的可以点击 此处 或者 此处 来进一步了解优化点。

六、最佳实践

本次学习总结出如下最佳实践:

  • try-with-resources 構文を使用すると、例外がスローされるかどうかに関係なく、try-block の実行後にリソースの close メソッドが呼び出されます。
  • try-with-resources 構文を使用して複数のリソースを作成します。try-block の実行後に呼び出される close メソッドの順序は、リソース作成の順序とは逆になります。
  • try-block ブロックが例外をスローした後、try-with-resources 構文を使用します。
  • 最初にすべてのリソースの close メソッド (try() で宣言) を実行します。 次に、コード内で catch を実行します。そして最後に。
  • try の () で管理されるリソースの構築中にスローされた例外は、この try に対応する catch でキャッチされる必要があります。
  • 自動的に呼び出される close メソッドは、宣言された例外を表示します。この例外は、この try に対応する catch でキャプチャする必要があります。
  • try-catch-finally の try/catch/finally でそれぞれ例外がスローされ、どの例外をスローするかを決定できない場合は、対応する例外を try でスローして Throwable を渡す必要があります。彼らの間の関係。

関連する無料学習の推奨事項: Java 基本チュートリアル

以上がJava の AutoCloseable インターフェイスの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はjuejin.imで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。