JDK9新特性实战:简化流关闭新姿势
轉載自?JDK9新特性實戰:簡化流關閉新姿勢。
做Java開發的都知道,每個資源的打開都需要對應的關閉操作,不然就會使資源一直占用而造成資源浪費,從而降低系統性能。
關于資源的關閉操作,從JDK7-JDK9有了不少的提升及簡化。
JDK6
在JDK6及之前,每個資源都需要我們手動寫代碼關閉,如:
FileInputStream fis = null; byte[] buffer = new byte[1024]; try {fis = new FileInputStream(new File("E:\\Java技術棧.txt"));while (fis.read(buffer) > 0) {System.out.println(new String(buffer));} } catch (Exception e) {e.printStackTrace(); } finally {if (fis != null) {try {fis.close();} catch (IOException e) {e.printStackTrace();}} }資源打開使用完后,必須在finally塊中進行手動關閉!我們有的同事,做一個查詢功能點,打開了連接查詢完后沒有手動關閉,最后造成連接池超出最大連接數而使系統功能堵塞。
JDK7
JDK7發布后,添加了新特性:try-with-resources語句。所有需要關閉的資源只要實現了 java.lang.AutoCloseable(java.io.Closeable就實現了這個接口)接口就在會程序結束后自動關閉。
如上面的讀取文件的流程序用JDK7來寫:
byte[] buffer = new byte[1024]; try (FileInputStream fis = new FileInputStream(new File("E:\\Java技術棧.txt"))) {while (fis.read(buffer) > 0) {System.out.println(new String(buffer));} } catch (Exception e) {e.printStackTrace(); }所有的資源在 try()里面定義,并去掉了finally模塊。
下面我們來寫一個自定義的流來看看是否自動關閉了。
定義一個自定義輸入輸出流
class MyInputStream implements AutoCloseable { ? ?void read(String content) {System.out.println("read content " + content);} ? ?@Overridepublic void close() throws Exception {System.out.println("input stream is closed.");} } class MyOutputStream implements AutoCloseable { ? ?void write(String content) {System.out.println("write content " + content);} ? ?@Overridepublic void close() throws Exception {System.out.println("out stream is closed.");} }單個資源自動關閉
try (MyInputStream mis = new MyInputStream()) {mis.read("7_2"); } catch (Exception e) {e.printStackTrace(); }輸出:
read content 7_2
input stream is closed.
多個資源自動關閉
try()里面可以定義多個資源,它們的關閉順序是最后在 try()定義的資源先關閉。
try (MyInputStream mis = new MyInputStream(); MyOutputStream mos = new MyOutputStream()) {mis.read("7_3");mos.write("7_3"); } catch (Exception e) {e.printStackTrace(); }輸出:
read content 73
write content 73
out stream is closed.
input stream is closed.
JDK9
JDK9發布后,又簡化了try-with-resources語句的用法。
try()里面可以是一個變量,但必須是final的或者等同final才行。如下面的mis,mos定義成局部變量可以不用final,局部變量可以等同于final,但定義成成員變量就必須是用final修飾的,不然會編譯錯誤。
MyInputStream mis = new MyInputStream(); MyOutputStream mos = new MyOutputStream(); try (mis; mos) {mis.read("1.9");mos.write("1.9"); } catch (Exception e) {e.printStackTrace(); }輸出:
read content 1.9
write content 1.9
out stream is closed.
input stream is closed.
再來看個例子:
Connection dbCon = DriverManager.getConnection("url", "user", "password"); try (dbCon; ResultSet rs = dbCon.createStatement().executeQuery("select * from emp")) {while (rs.next()) {System.out.println("In loadDataFromDB() =====>>>>>>>>>>>> " + rs.getString(1));} } catch (SQLException e) {System.out.println("Exception occurs while reading the data from DB ->" + e.getMessage()); }dbCon和rs都能被自動關閉。
JKD9雖然簡化了,但感覺還是沒有什么質的變化,實際用途我們可能不希望關心資源的關閉,或者在方法結束之后如果是局部變量它就能自動關閉?;蛟S是我站的高度不夠,官方有其他的考量,但JDK9的這一點變化還是非常有用的。
總結
以上是生活随笔為你收集整理的JDK9新特性实战:简化流关闭新姿势的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高考在几月几日 高考在哪一天
- 下一篇: 水为什么能灭火 水能灭火的原因