当前位置:网站首页>Don't release resources in finally, unlock a new pose!
Don't release resources in finally, unlock a new pose!
2020-11-08 13:47:00 【osc_zq2o0u6t】
copyright : This article is original by blogger , Reprint please indicate the source .
Original address : https://blog.csdn.net/qq_38688267/article/details/109511716
In our coding process , Inevitably, it will be used in file operations IO flow 、 Database connection and other expensive resources , You need to go through close Method to turn it off , Otherwise, the resource is always open , May cause memory leaks and other problems .
Take the file manipulation flow for example , We need to use try catch
, Used up in finally
Closed in , And when it's closed, it needs to be try catch
, It can be said that it is very troublesome ! The code is as follows :
/** The traditional way of writing **/
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(""));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
// DO something
} finally {
if(br != null) {
try {
br.close();
} catch (IOException e) {
// DO something
}
}
}
And our new posture is to use JDK1.7 Medium try-with-resources
grammar , Go straight to the code :
/** Use grammar sugar **/
try (BufferedReader br1 = new BufferedReader(new FileReader(""))) {
String line;
while ((line = br1.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
// DO something
}
Is the code refreshing in an instant ? Use it quickly ~
Let's get to the bottom of it by the way , Take a look at what it looks like after it's compiled :
try {
BufferedReader br1 = new BufferedReader(new FileReader(""));
Throwable var7 = null;
try {
String line;
try {
while((line = br1.readLine()) != null) {
System.out.println(line);
}
} catch (Throwable var32) {
var7 = var32;
throw var32;
}
} finally {
if (br1 != null) {
if (var7 != null) {
try {
br1.close();
} catch (Throwable var31) {
var7.addSuppressed(var31);
}
} else {
br1.close();
}
}
}
} catch (IOException var34) {
}
In fact, the principle behind it is very simple , Let the compiler do the work of closing resources for us . therefore , It is confirmed again , The function of grammar sugar is to facilitate the use of programmers , Finally, it has to be transformed into the language that the compiler knows .
I hope this article can be helpful or enlightening for you . It's not easy to code words , I think it's good to write, so I can support it ~
版权声明
本文为[osc_zq2o0u6t]所创,转载请带上原文链接,感谢
边栏推荐
- What is the database paradigm
- 模板引擎的整理归纳
- Introduction to mongodb foundation of distributed document storage database
- 我用 Python 找出了删除我微信的所有人并将他们自动化删除了
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- Ali! Visual computing developer's series of manuals (with internet disk link)
- 用科技赋能教育创新与重构 华为将教育信息化落到实处
- Entry level! Teach you how to develop small programs without asking for help (with internet disk link)
- Flink from introduction to Zhenxiang (10. Sink data output elasticsearch)
- Share the experience of passing the PMP examination
猜你喜欢
用科技赋能教育创新与重构 华为将教育信息化落到实处
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Q & A and book giving activities of harbor project experts
This paper analyzes the top ten Internet of things applications in 2020!
Flink从入门到真香(10、Sink数据输出-Elasticsearch)
Returning to the third place in the world, what did Xiaomi do right?
最全!阿里巴巴经济体云原生实践!(附网盘链接)
Python basic syntax
The progress bar written in Python is so wonderful~
阿里云加速增长,进一步巩固领先优势
随机推荐
It's worth seeing! EMR elastic low cost offline big data analysis best practice (with network disk link)
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
在51CTO学院Get到PMP证书
Flink从入门到真香(7、Sink数据输出-文件)
Python基础语法
10 common software architecture patterns
Golang system ping program to detect the surviving host (any permission)
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Python basic syntax
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Or talk No.19 | Facebook Dr. Tian Yuandong: black box optimization of hidden action set based on Monte Carlo tree search
Ali teaches you how to use the Internet of things platform! (Internet disk link attached)
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
This paper analyzes the top ten Internet of things applications in 2020!
Blockchain weekly: the development of digital currency is written into the 14th five year plan; Biden invited senior adviser of MIT digital currency program to join the presidential transition team; V
android基础-CheckBox(复选框)
后端程序员必备:分布式事务基础篇
The progress bar written in Python is so wonderful~
Tencent, which is good at to C, how to take advantage of Tencent's cloud market share in these industries?
Why is Schnorr Signature known as the biggest technology update after bitcoin segwit