一篇文章带你深入理解 try-with-resource

一、为什么引入 try-with-resource?

所有被打开的系统资源,比如流、文件或者Socket连接等,都需要被开发者手动关闭,否则随着程序的不断运行,资源泄露将会累积成重大的生产事故。

当然可以将处理资源关闭的代码写在finally块中。

然而,如果你同时打开了多个资源,那么将会出现噩梦般的场景:

public class Demo {
    public static void main(String[] args) {
        BufferedInputStream bin = null;
        BufferedOutputStream bout = null;
        try {
            bin = new BufferedInputStream(new FileInputStream(new File("E:\\in.txt")));
            bout = new BufferedOutputStream(new FileOutputStream(new File("E:\\out.txt")));
            int b;
            while ((b = bin.read()) != -1) {
                bout.write(b);
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (bin != null) {
                try {
                    bin.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
                finally {
                    if (bout != null) {
                        try {
                            bout.close();
                        }
                        catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

关闭资源的代码竟然比业务代码还要多 !!!

这是因为,我们不仅需要关闭 BufferedInputStream ,还需要保证如果关闭 BufferedInputStream 时出现了异常, BufferedOutputStream 也要能被正确地关闭。所以我们不得不借助finally中嵌套finally大法。

可以想到,打开的资源越多,finally中嵌套的将会越深!!!

所以引入了 Try-with-resourse

Try-with-resourse 语句类似于 Python中 的 with 语句,都是自动释放资源,而不用像传统的 try-catch-finally一样必须使用finally关闭资源,而且当资源释放比较多的时候,会出现嵌套关闭资源的现象.

二、try-with-resource 的使用

现在使用 try-with-resource 对上面的例子进行优化

public class Demo {
    public static void main(String[] args) {
        try (BufferedInputStream bin = new BufferedInputStream(new FileInputStream(new File("E:\\in.txt")));
             BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(new File("E:\\out.txt")))) {
            int b;
            while ((b = bin.read()) != -1) {
                bout.write(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

try-with-resources 声明在 JDK 9 已得到改进。

如果你已经有一个资源是 final 或等效于 final 变量,您可以在 try-with-resources 语句中使用该变量,而无需在 try-with-resources 语句中声明一个新变量。

public class Demo {
    public static void main(String[] args) throws FileNotFoundException {
        BufferedInputStream bin = new BufferedInputStream(new FileInputStream(new File("E:\\in.txt")));
        BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(new File("E:\\out.txt")));
        try (bin;bout) {
            int b;
            while ((b = bin.read()) != -1) {
                bout.write(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

代码变得更为简洁了

三、原理探究

为了能够配合 try-with-resource,资源必须实现 AutoClosable 接口。

该接口的实现类需要重写 close 方法:

public class Connection implements AutoCloseable{
    public void sendData(){
        System.out.println("正在发送数据");
    }

    @Override
    public void close() throws Exception {
        System.out.println("正在关闭连接");
    }
}

调用类:

public class TryWithResource {
    public static void main(String[] args) {
        Connection connection = new Connection();
        try (connection) {
            connection.sendData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

结果:

正在发送数据
正在关闭连接

我们反编译刚才例子的class文件:

package com.codersm.trywithresource;

public class TryWithResource {
  public TryWithResource() {
  }

  public static void main(String[] args) {
    try {
      Connection conn = new Connection();
      Throwable var2 = null;

      try {
        conn.sendData();
      } catch (Throwable var12) {
        var2 = var12;
        throw var12;
      } finally {
        if (conn != null) {
          if (var2 != null) {
            try {
              conn.close();
            } catch (Throwable var11) {
              var2.addSuppressed(var11);
            }
          } else {
            conn.close();
          }
        }

      }
    } catch (Exception var14) {
      var14.printStackTrace();
    }

  }
}

看到没,在第15~27行,编译器自动帮我们生成了finally块,并且在里面调用了资源的close方法,所以例子中的close方法会在运行的时候被执行。

异常屏蔽:

刚才反编译的代码(第23行)多了一个 addSuppressed 。为了了解这段代码的用意,我们稍微修改一下刚才的例子:我们将刚才的代码改回远古时代手动关闭异常的方式,并且在 sendData 和 close 方法中抛出异常:

public class Connection implements AutoCloseable {
  public void sendData() throws Exception {
    throw new Exception("send data");
  }
  @Override
  public void close() throws Exception {
    throw new MyException("close");
  }
}

修改 main 方法:

public class TryWithResource {
  public static void main(String[] args) {
    try {
      test();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
  private static void test() throws Exception {
    Connection conn = null;
    try {
      conn = new Connection();
      conn.sendData();
    }
    finally {
      if (conn != null) {
        conn.close();
      }
    }
  }
}

运行之后我们发现:

basic.exception.MyException: close
at basic.exception.Connection.close(Connection.java:10)
at basic.exception.TryWithResource.test(TryWithResource.java:82)
at basic.exception.TryWithResource.main(TryWithResource.java:7)

好的,问题来了,由于我们一次只能抛出一个异常,所以在最上层看到的是最后一个抛出的异常——也就是 close 方法抛出的 MyException ,而 sendData 抛出的 Exception 被忽略了。

这就是所谓的异常屏蔽

由于异常信息的丢失,异常屏蔽可能会导致某些bug变得极其难以发现,程序员们不得不加班加点地找bug,如此毒瘤,怎能不除!

幸好,为了解决这个问题,从Java 1.7开始,大佬们为 Throwable 类新增了 addSuppressed 方法,支持将一个异常附加到另一个异常身上,从而避免异常屏蔽。

那么被屏蔽的异常信息会通过怎样的格式输出呢?我们再运行一遍刚才用try-with-resource包裹的main方法:

java.lang.Exception: send data

 at basic.exception.Connection.sendData(Connection.java:5)
 at basic.exception.TryWithResource.main(TryWithResource.java:14)
 ......
 Suppressed: basic.exception.MyException: close
 at basic.exception.Connection.close(Connection.java:10)
 at basic.exception.TryWithResource.main(TryWithResource.java:15)
 ... 5 more

可以看到,异常信息中多了一个 Suppressed 的提示,告诉我们这个异常其实由两个异常组成, MyException 是被Suppressed的异常。

四、注意事项

在使用try-with-resource的过程中,一定需要了解资源的 close 方法内部的实现逻辑。

否则还是可能会导致资源泄露。

举个例子,在Java BIO中采用了大量的装饰器模式。

当调用装饰器的 close 方法时,本质上是调用了装饰器内部包裹的流的 close 方法。

比如:

public class TryWithResource {
  public static void main(String[] args) {
    try (FileInputStream fin = new FileInputStream(new File("input.txt"));
        GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(new File("out.txt")))) {
      byte[] buffer = new byte[4096];
      int read;
      while ((read = fin.read(buffer)) != -1) {
        out.write(buffer, 0, read);
      }
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }
}

在上述代码中,我们从 FileInputStream 中读取字节,并且写入到 GZIPOutputStream 中。

GZIPOutputStream 实际上是 FileOutputStream 的装饰器。

由于try-with-resource的特性,实际编译之后的代码会在后面带上finally代码块,并且在里面调用fin.close()方法和out.close()方法。

我们再来看 GZIPOutputStream 类的close方法:

public void close() throws IOException {
  if (!closed) {
    finish();
    if (usesDefaultDeflater)
      def.end();
    out.close();
    closed = true;
  }
}

我们可以看到,out变量实际上代表的是被装饰的 FileOutputStream 类。

在调用out变量的 close 方法之前, GZIPOutputStream 还做了 finish 操作,该操作还会继续往 FileOutputStream 中写压缩信息,

此时如果出现异常,则会 out.close() 方法被略过,然而这个才是最底层的资源关闭方法。

正确的做法是应该在try-with-resource中单独声明最底层的资源,保证对应的 close 方法一定能够被调用。

在刚才的例子中,我们需要单独声明每个 FileInputStream 以及 FileOutputStream :

public class TryWithResource {
  public static void main(String[] args) {
    try (FileInputStream fin = new FileInputStream(new File("input.txt"));
        FileOutputStream fout = new FileOutputStream(new File("out.txt"));
        GZIPOutputStream out = new GZIPOutputStream(fout)) {
      byte[] buffer = new byte[4096];
      int read;
      while ((read = fin.read(buffer)) != -1) {
        out.write(buffer, 0, read);
      }
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }
}

由于编译器会自动生成 fout.close() 的代码,这样肯定能够保证真正的流被关闭。

### 回答1: try-with-resourceJava SE7中引入的一个新特性,它可以简化资源管理的代码,同时也能够确保程序在完成资源使用后,能够正确地关闭资源。 在使用try-with-resource语句时,需要将要使用的资源对象放在try关键字的括号中。这个资源对象必须是实现了java.lang.AutoCloseable接口的类的对象。在try语句块执行完毕后,不需要手动关闭资源对象,try-with-resource语句会自动调用资源对象的close()方法来关闭资源。这样可以避免忘记关闭资源或者错误地关闭资源所导致的问题。 下面是一个使用try-with-resource的例子,假设我们要读取一个文件的内容并打印出来: ``` try (BufferedReader reader = new BufferedReader(new FileReader("file.txt"))) { String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { // handle exception } ``` 在这个例子中,我们使用了try-with-resource语句来创建一个BufferedReader对象,并在try语句块中使用它来读取文件的内容。当try语句块执行完毕后,程序会自动关闭BufferedReader对象。如果在读取文件的过程中发生了异常,程序也会正确地处理异常并关闭资源。 ### 回答2: try-with-resource 语句是Java 7中引入的一种语法结构,用于在处理资源时自动关闭资源。在传统的Java代码中,我们使用try-catch-finally语句来处理资源的释放,但这种方式很容易导致遗漏关闭资源的情况。而try-with-resource语句则可以更简洁地处理这个问题。 try-with-resource语句的语法如下: ```java try (resource declaration) { // code that might throw an exception } catch (exception type) { // exception handling code } ``` 在try-with-resource语句中,我们可以在try关键字后面的括号中声明资源,这个资源必须是实现了AutoCloseable接口的类的实例。当try块执行完毕后,不论是正常执行还是发生异常,系统会自动调用该资源的close()方法来关闭资源,无需显式编写关闭资源的代码。 这种语法的好处在于,无论代码是否抛出异常,资源都能正确关闭,避免了资源泄漏的问题。此外,由于资源的关闭由系统自动处理,代码也更简洁清晰。 需要注意的是,try-with-resource语句中可以声明多个资源,多个资源之间使用分号分隔。资源的声明顺序决定了关闭顺序,即先声明的资源会后关闭。 总结而言,try-with-resource语句是一种在处理资源时自动关闭资源的语法结构。它不仅可以减少代码量,更重要的是能够确保资源的正确关闭,保证代码的健壮性和可靠性。 ### 回答3: try-with-resourceJava中的一种异常处理机制,用于自动关闭资源,确保资源正确关闭,同时减少了代码的冗余和异常处理的复杂性。 在Java 7中引入了try-with-resource语句,主要用于自动关闭实现了AutoCloseable接口的资源。使用try-with-resource语句能够简化资源关闭的代码,并且能够在try块执行完毕后自动关闭资源,不需要显式调用close方法。 try-with-resource语句的语法如下: try (资源初始化) { // 代码块 } catch (异常类型 异常对象) { // 异常处理 } finally { // 无需手动关闭资源 } 在try-with-resource语句中,资源的初始化在try关键字之后的括号中进行,可以同时初始化多个资源,多个资源之间使用分号分隔。 在执行try块里面的代码时,如果发生异常,会首先执行catch块中的异常处理代码,当catch块执行完毕后,会自动关闭初始化的资源,即使catch块中也发生了异常。而在try块里面如果没有发生异常,则会直接执行finally块中的代码,并在执行完毕后自动关闭资源。 使用try-with-resource语句可以有效地处理资源的关闭问题,避免资源泄漏和忘记手动关闭资源的情况发生。同时,由于try-with-resource语句能自动关闭资源,简化了代码的编写,更容易理解和维护。不过要注意,资源对象必须实现了AutoCloseable接口才能使用try-with-resource语句。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值