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

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

JDK7 之前的资源关闭方式

/**
* jdk7以前关闭流的方式
* */
public class CloseResourceBefore7 {
private static final String FileName = "file.txt"; public static void main(String[] args) throws IOException {
FileInputStream inputStream = null; try {
inputStream = new FileInputStream(FileName);
char c1 = (char) inputStream.read();
System.out.println("c1=" + c1);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
}

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

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

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

1. 一个例子

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

/**
* 资源类
* */
public class Resource implements AutoCloseable {
public void sayHello() {
System.out.println("hello");
} @Override
public void close() throws Exception {
System.out.println("Resource is closed");
}
}

测试类 CloseResourceIn7.java

/**
* jdk7及以后关闭流的方式
* */
public class CloseResourceIn7 {
public static void main(String[] args) {
try(Resource resource = new Resource()) {
resource.sayHello();
} catch (Exception e) {
e.printStackTrace();
}
}
}

打印结果:

hello
Resource is closed

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

/**
* 资源2
* */
public class Resource2 implements AutoCloseable {
public void sayhello() {
System.out.println("Resource say hello");
} @Override
public void close() throws Exception {
System.out.println("Resource2 is closed");
}
}

测试类 CloseResourceIn7.java

/**
* jdk7及以后关闭流的方式
* */
public class CloseResourceIn7 {
public static void main(String[] args) {
try(Resource resource = new Resource(); Resource2 resource2 = new Resource2()) {
resource.sayHello();
resource2.sayhello();
} catch (Exception e) {
e.printStackTrace();
}
}
}

结果:

hello
hello2
Resource2 is closed
Resource is closed

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

原理

查看编译的 class 文件 CloseResourceIn7.class

public class CloseResourceIn7 {
public CloseResourceIn7() {
} public static void main(String[] args) {
try {
Resource resource = new Resource();
Throwable var2 = null;
try {
resource.sayHello();
} catch (Throwable var12) {
var2 = var12;
throw var12;
} finally {
if (resource != null) {
if (var2 != null) {
try {
resource.close();
} catch (Throwable var11) {
var2.addSuppressed(var11);
}
} else {
resource.close();
}
}
}
} catch (Exception var14) {
var14.printStackTrace();
}
}
}

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

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

/**
* 资源类
* */
public class Resource implements AutoCloseable {
public void sayHello() throws Exception {
throw new Exception("Resource throw Exception");
} @Override
public void close() throws Exception {
throw new Exception("Close method throw Exception");
}
}

测试类 CloseResourceBefore7.java

/**
* jdk7以前关闭流的方式
* */
public class CloseResourceBefore7 { public static void main(String[] args) {
try {
errorTest();
} catch (Exception e) {
e.printStackTrace();
}
} private static void errorTest() throws Exception {
Resource resource = null;
try {
resource = new Resource();
resource.sayHello();
} finally {
if (resource != null) {
resource.close();
}
}
}
}

打印结果:

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

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

/**
* jdk7及以后关闭流的方式
* */
public class CloseResourceIn7 { public static void main(String[] args) {
try {
errorTest();
} catch (Exception e) {
e.printStackTrace();
}
} private static void errorTest() throws Exception {
try(Resource resource = new Resource()) {
resource.sayHello();
} }
}

打印信息:

java.lang.Exception: Resource throw Exception
at com.shuwen.Resource.sayHello(Resource.java:10)
at com.shuwen.CloseResourceIn7.errorTest(CloseResourceIn7.java:20)
at com.shuwen.CloseResourceIn7.main(CloseResourceIn7.java:12)
Suppressed: java.lang.Exception: Close method throw Exception
at com.shuwen.Resource.close(Resource.java:15)
at com.shuwen.CloseResourceIn7.errorTest(CloseResourceIn7.java:21)
... 1 more

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

应用:在 Jedis 中的使用

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; public class JedisTest { public static void main(String[] args) {
JedisPool pool = new JedisPool();
try (Jedis jedis = pool.getResource()) { // 用完自动 close
doSomething(jedis);
}
} private static void doSomething(Jedis jedis) {
// code it here
}
}

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

使用 try-with-resources 优雅关闭资源的更多相关文章

  1. Effective java 系列之更优雅的关闭资源-try-with-resources

    背景: 在Java编程过程中,如果打开了外部资源(文件.数据库连接.网络连接等),我们必须在这些外部资源使用完毕后,手动关闭它们.因为外部资源不由JVM管理,无法享用JVM的垃圾回收机制,如果我们不在 ...

  2. 更优雅地关闭资源 - try-with-resource及其异常抑制

    原文:https://www.cnblogs.com/itZhy/p/7636615.html 一.背景 我们知道,在Java编程过程中,如果打开了外部资源(文件.数据库连接.网络连接等),我们必须在 ...

  3. Java进阶知识点:更优雅地关闭资源 - try-with-resource

    一.背景 我们知道,在Java编程过程中,如果打开了外部资源(文件.数据库连接.网络连接等),我们必须在这些外部资源使用完毕后,手动关闭它们.因为外部资源不由JVM管理,无法享用JVM的垃圾回收机制, ...

  4. Java进阶知识点3:更优雅地关闭资源 - try-with-resource及其异常抑制

    一.背景 我们知道,在Java编程过程中,如果打开了外部资源(文件.数据库连接.网络连接等),我们必须在这些外部资源使用完毕后,手动关闭它们.因为外部资源不由JVM管理,无法享用JVM的垃圾回收机制, ...

  5. 更优雅地关闭资源 - try-with-resource及其异常抑制--转载

    原文地址:https://www.cnblogs.com/itZhy/p/7636615.html 一.背景 我们知道,在Java编程过程中,如果打开了外部资源(文件.数据库连接.网络连接等),我们必 ...

  6. C++中没有finally,那么应该在如何关闭资源

    这是一篇有趣的帖子 原文链接: http://bbs.csdn.net/topics/90070457 楼主: C++中没有finally,那么应该在哪里关闭资源? C++的try{}catch(){ ...

  7. C++中没有finally,那么应该在哪里关闭资源?

    这是一篇有趣的帖子 原文链接: http://bbs.csdn.net/topics/90070457 楼主: C++中没有finally,那么应该在哪里关闭资源? C++的try{}catch(){ ...

  8. Socket编程中的强制关闭与优雅关闭及相关socket选项

    以下描述主要是针对windows平台下的TCP socket而言. 首先需要区分一下关闭socket和关闭TCP连接的区别,关闭TCP连接是指TCP协议层的东西,就是两个TCP端之间交换了一些协议包( ...

  9. 文件的读取与保存(try-with-resource优雅关闭)

    借鉴:https://www.cnblogs.com/itZhy/p/7636615.html 一.背景 在Java编程过程中,如果打开了外部资源(文件.数据库连接.网络连接等),我们必须在这些外部资 ...

随机推荐

  1. storm on yarn(CDH5) 部署笔记

    按照storm on yarn(Apache hadoop)部署好之后,然后修改HADOOP_HOME,hadoopenv.sh中的JAVA_HOME,以及storm-yarn-master中pom. ...

  2. 经验分享:Essay写作遇到困难请你这样做

    很多留学生在essay写作中可能会遇到很多困难,要么是essay写作内容出现问题,又或者是对于essay写作格式的不了解,导致自己无法顺利完成essay.今天小编就收集了几位留学生的写作经验分享,希望 ...

  3. Flink与Spark Streaming在与kafka结合的区别!

    本文主要是想聊聊flink与kafka结合.当然,单纯的介绍flink与kafka的结合呢,比较单调,也没有可对比性,所以的准备顺便帮大家简单回顾一下Spark Streaming与kafka的结合. ...

  4. js 循环与判断语句的几个练习

    <script type="text/javascript"> /*1.X3 * 6528 = 3X * 8256 X为一个数字 填入一个数字 使等式成立*/ for ...

  5. UVA 11404 简单LCS模型DP 字典序比较

    这个题目求某个字符串中含的最长的回文子串. 就是一个很简单的LCS模型吗,而且我不明白为什么网上这么多人都说仿照某写法把字符串先逆序一下,然后求LCS,我只想问一下,有必要吗? 直接按LCS的套路来就 ...

  6. windows下MariaDB忘记密码找回

    1.首先停止数据库 2.找到my.ini文件 3.右键以记事本打开在Mysqld下添加如下一行保存,然后启动数据库 4.登录数据库会提示输入密码,默认回车即可 5.退出数据库,删除我们上面在my.in ...

  7. 吴裕雄--天生自然 JAVASCRIPT开发学习:Number 对象

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. Django2.0——模板渲染(一)

    在前面的介绍中我们都是用简单的 django.http.HttpResponse来把内容显示到网页上,本节将讲解如何使用渲染模板的方法来显示内容,即调用精美的HTML页面.模板的创建既可以在项目下创建 ...

  9. Hadoop的FlieSystem类的使用

    1.使用FileSystem类需要导入jar包 解压hadoop-2.7.7.tar.gz 复制如下三个jar包和lib下所有jar包到项目文件下的lib文件 2.查看文件信息 @Test publi ...

  10. LeetCode——560. 和为K的子数组

    给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] 与 [1,1] 为两种不 ...