使用 try-with-resources 優(yōu)雅關(guān)閉資源

我們知道,在 Java 編程過(guò)程中,如果打開了外部資源(文件、數(shù)據(jù)庫(kù)連接、網(wǎng)絡(luò)連接等、redis),我們必須在這些外部資源使用完畢后,手動(dòng)關(guān)閉它們。

因?yàn)橥獠抠Y源不由 JVM 管理,無(wú)法享用 JVM 的垃圾回收機(jī)制,如果我們不在編程時(shí)確保在正確的時(shí)機(jī)關(guān)閉外部資源,就會(huì)導(dǎo)致外部資源泄露,緊接著就會(huì)出現(xiàn)文件被異常占用,數(shù)據(jù)庫(kù)連接過(guò)多導(dǎo)致連接池溢出**等諸多很嚴(yán)重的問(wèn)題。

JDK7 之前的資源關(guān)閉方式

/**
 * jdk7以前關(guān)閉流的方式
 * */
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 語(yǔ)法關(guān)閉

在 JDK7 以前,Java 沒(méi)有自動(dòng)關(guān)閉外部資源的語(yǔ)法特性,直到 JDK7 中新增了try-with-resource 語(yǔ)法,才實(shí)現(xiàn)了這一功能。

try-with-resource Resource 的定義:所有實(shí)現(xiàn)了 java.lang.AutoCloseable 接口(其中,它包括實(shí)現(xiàn)了 java.io.Closeable 的所有對(duì)象),可以使用作為資源。

1. 一個(gè)例子

一個(gè)實(shí)現(xiàn)了 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");
    }
}

測(cè)試類 CloseResourceIn7.java

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

打印結(jié)果:

hello
Resource is closed

當(dāng)存在多個(gè)打開資源的時(shí)候: 資源二 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");
    }
}

測(cè)試類 CloseResourceIn7.java

/**
 * jdk7及以后關(guān)閉流的方式
 * */
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();
        }
    }
}

結(jié)果:

hello
hello2
Resource2 is closed
Resource is closed

注意:打開多個(gè)資源的時(shí)候,關(guān)閉順序是倒敘的

原理

查看編譯的 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();
        }
    }
}

可以發(fā)現(xiàn)編譯以后生成了 try-catch-finally 語(yǔ)句塊 finally 中的 var2.addSuppressed(var11),這么做是為了處理異常屏蔽的。

我們將代碼修改一下 資源 Resource.java,讓兩個(gè)方法里都拋出異常

/**
 * 資源類
 * */
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");
    }
}

測(cè)試類 CloseResourceBefore7.java

/**
 * jdk7以前關(guān)閉流的方式
 * */
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();
            }
        }
    }
}

打印結(jié)果:

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)

只打印了最后出現(xiàn)的關(guān)閉異?!井惓F帘巍窟@樣會(huì)給開發(fā)人員排查錯(cuò)誤帶來(lái)一定的困難 我們換成 try-with-resource 方法實(shí)現(xiàn)CloseResourceIn7.java

/**
 * jdk7及以后關(guān)閉流的方式
 * */
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

可以發(fā)現(xiàn),異常信息中多了一個(gè) Suppressed 的提示,告訴我們這個(gè)異常其實(shí)由兩個(gè)異常組成,Close method throw Exception這個(gè)異常是被 Suppressed【屏蔽】的異常。

應(yīng)用:在 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()) { // 用完自動(dòng) close
      doSomething(jedis);
    }
  }

  private static void doSomething(Jedis jedis) {
    // code it here
  }
}

這樣 Jedis 對(duì)象肯定會(huì)歸還給連接池 (死循環(huán)除外),避免應(yīng)用程序卡死的慘劇發(fā)生。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容