我们知道,在 Java 编程过程中,如果打开了外部资源(文件、数据库连接、网络连接等、redis),我们必须在这些外部资源使用完毕后,手动关闭它们。

因为外部资源不由 JVM 管理,无法享用 JVM 的垃圾回收机制,如果我们不在编程时确保在正确的时机关闭外部资源,就会导致外部资源泄露,紧接着就会出现文件被异常占用,数据库连接过多导致连接池溢出**等诸多很严重的问题。

JDK7 之前的资源关闭方式

  1. /**
  2. * jdk7以前关闭流的方式
  3. * */
  4. public class CloseResourceBefore7 {
  5. private static final String FileName = "file.txt";
  6. public static void main(String[] args) throws IOException {
  7. FileInputStream inputStream = null;
  8. try {
  9. inputStream = new FileInputStream(FileName);
  10. char c1 = (char) inputStream.read();
  11. System.out.println("c1=" + c1);
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. } finally {
  15. if (inputStream != null) {
  16. inputStream.close();
  17. }
  18. }
  19. }
  20. }

JDK7 之后用 try-with-resource 语法关闭

在 JDK7 以前,Java 没有自动关闭外部资源的语法特性,直到 JDK7 中新增了try-with-resource 语法,才实现了这一功能。

try-with-resource Resource 的定义:所有实现了 java.lang.AutoCloseable 接口(其中,它包括实现了 java.io.Closeable 的所有对象),可以使用作为资源。

一个例子

一个实现了 java.lang.AutoCloseable 接口的 Resource 类:

  1. /**
  2. * 资源类
  3. * */
  4. public class Resource implements AutoCloseable {
  5. public void sayHello() {
  6. System.out.println("hello");
  7. }
  8. @Override
  9. public void close() throws Exception {
  10. System.out.println("Resource is closed");
  11. }
  12. }

测试类 CloseResourceIn7.java

  1. /**
  2. * jdk7及以后关闭流的方式
  3. * */
  4. public class CloseResourceIn7 {
  5. public static void main(String[] args) {
  6. try(Resource resource = new Resource()) {
  7. resource.sayHello();
  8. } catch (Exception e) {
  9. e.printStackTrace();
  10. }
  11. }
  12. }

打印结果:

  1. hello
  2. Resource is closed

当存在多个打开资源的时候: 资源二 Resource2.java

  1. /**
  2. * 资源2
  3. * */
  4. public class Resource2 implements AutoCloseable {
  5. public void sayhello() {
  6. System.out.println("Resource say hello");
  7. }
  8. @Override
  9. public void close() throws Exception {
  10. System.out.println("Resource2 is closed");
  11. }
  12. }

测试类 CloseResourceIn7.java

  1. /**
  2. * jdk7及以后关闭流的方式
  3. * */
  4. public class CloseResourceIn7 {
  5. public static void main(String[] args) {
  6. try(Resource resource = new Resource(); Resource2 resource2 = new Resource2()) {
  7. resource.sayHello();
  8. resource2.sayhello();
  9. } catch (Exception e) {
  10. e.printStackTrace();
  11. }
  12. }
  13. }

结果:

  1. hello
  2. hello2
  3. Resource2 is closed
  4. Resource is closed

注意:打开多个资源的时候,关闭顺序是倒叙的

原理

查看编译的 class 文件 CloseResourceIn7.class

  1. public class CloseResourceIn7 {
  2. public CloseResourceIn7() {
  3. }
  4. public static void main(String[] args) {
  5. try {
  6. Resource resource = new Resource();
  7. Throwable var2 = null;
  8. try {
  9. resource.sayHello();
  10. } catch (Throwable var12) {
  11. var2 = var12;
  12. throw var12;
  13. } finally {
  14. if (resource != null) {
  15. if (var2 != null) {
  16. try {
  17. resource.close();
  18. } catch (Throwable var11) {
  19. var2.addSuppressed(var11);
  20. }
  21. } else {
  22. resource.close();
  23. }
  24. }
  25. }
  26. } catch (Exception var14) {
  27. var14.printStackTrace();
  28. }
  29. }
  30. }

可以发现编译以后生成了 try-catch-finally 语句块 finally 中的 var2.addSuppressed(var11),这么做是为了处理异常屏蔽的。

我们将代码修改一下 资源 Resource.java,让两个方法里都抛出异常

  1. /**
  2. * 资源类
  3. * */
  4. public class Resource implements AutoCloseable {
  5. public void sayHello() throws Exception {
  6. throw new Exception("Resource throw Exception");
  7. }
  8. @Override
  9. public void close() throws Exception {
  10. throw new Exception("Close method throw Exception");
  11. }
  12. }

测试类 CloseResourceBefore7.java

  1. /**
  2. * jdk7以前关闭流的方式
  3. * */
  4. public class CloseResourceBefore7 {
  5. public static void main(String[] args) {
  6. try {
  7. errorTest();
  8. } catch (Exception e) {
  9. e.printStackTrace();
  10. }
  11. }
  12. private static void errorTest() throws Exception {
  13. Resource resource = null;
  14. try {
  15. resource = new Resource();
  16. resource.sayHello();
  17. }
  18. finally {
  19. if (resource != null) {
  20. resource.close();
  21. }
  22. }
  23. }
  24. }

打印结果:

  1. java.lang.Exception: Close method throw Exception
  2. at com.shuwen.Resource.close(Resource.java:15)
  3. at com.shuwen.CloseResourceIn7.errorTest(CloseResourceIn7.java:27)
  4. at com.shuwen.CloseResourceIn7.main(CloseResourceIn7.java:12)

只打印了最后出现的关闭异常【异常屏蔽】这样会给开发人员排查错误带来一定的困难 我们换成 try-with-resource 方法实现CloseResourceIn7.java

  1. /**
  2. * jdk7及以后关闭流的方式
  3. * */
  4. public class CloseResourceIn7 {
  5. public static void main(String[] args) {
  6. try {
  7. errorTest();
  8. } catch (Exception e) {
  9. e.printStackTrace();
  10. }
  11. }
  12. private static void errorTest() throws Exception {
  13. try(Resource resource = new Resource()) {
  14. resource.sayHello();
  15. }
  16. }
  17. }

打印信息:

  1. java.lang.Exception: Resource throw Exception
  2. at com.shuwen.Resource.sayHello(Resource.java:10)
  3. at com.shuwen.CloseResourceIn7.errorTest(CloseResourceIn7.java:20)
  4. at com.shuwen.CloseResourceIn7.main(CloseResourceIn7.java:12)
  5. Suppressed: java.lang.Exception: Close method throw Exception
  6. at com.shuwen.Resource.close(Resource.java:15)
  7. at com.shuwen.CloseResourceIn7.errorTest(CloseResourceIn7.java:21)
  8. ... 1 more

可以发现,异常信息中多了一个 Suppressed 的提示,告诉我们这个异常其实由两个异常组成,Close method throw Exception这个异常是被 Suppressed【屏蔽】的异常。

应用:在 Jedis 中的使用

  1. import redis.clients.jedis.Jedis;
  2. import redis.clients.jedis.JedisPool;
  3. public class JedisTest {
  4. public static void main(String[] args) {
  5. JedisPool pool = new JedisPool();
  6. try (Jedis jedis = pool.getResource()) { // 用完自动 close
  7. doSomething(jedis);
  8. }
  9. }
  10. private static void doSomething(Jedis jedis) {
  11. // code it here
  12. }
  13. }

这样 Jedis 对象肯定会归还给连接池 (死循环除外),避免应用程序卡死的惨剧发生。