当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 用科技赋能教育创新与重构 华为将教育信息化落到实处
- CSP考试须知与各种小技巧
- On monotonous stack
- 最全!阿里巴巴经济体云原生实践!(附网盘链接)
- 擅长To C的腾讯,如何借腾讯云在这几个行业云市场占有率第一?
- From a friend recently Ali, Tencent, meituan and other P7 Python development post interview questions
- 10个常见的软件架构模式
- AQS解析
- 2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
- Shell uses. Net objects to send mail
猜你喜欢
This time Kwai tiktok is faster than shaking.
Improvement of rate limit for laravel8 update
重返全球第三,小米做对了什么?
后端程序员必备:分布式事务基础篇
Ali! Visual computing developer's series of manuals (with internet disk link)
Workers, workers soul, draw lifelong members, become a person!
数据库连接报错之IO异常(The Network Adapter could not establish the connection)
Harbor项目高手问答及赠书活动
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
金融领域首个开源中文BERT预训练模型,熵简科技推出FinBERT 1.0
随机推荐
Flink从入门到真香(10、Sink数据输出-Elasticsearch)
来自朋友最近阿里、腾讯、美团等P7级Python开发岗位面试题
How to solve the difference between NAT IP and port IP
2020-11-05
Workers, workers soul, draw lifelong members, become a person!
别再在finally里面释放资源了,解锁个新姿势!
Essential for back-end programmers: distributed transaction Basics
供货紧张!苹果被曝 iPhone 12 电源芯片产能不足
Summary of template engine
PMP考试通过心得分享
This time Kwai tiktok is faster than shaking.
Flink从入门到真香(3、从集合和文件中读取数据)
Flink: from introduction to Zhenxiang (6. Flink implements UDF function - realizes more fine-grained control flow)
10个常见的软件架构模式
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Golang ICMP Protocol detects viable hosts
Understanding design patterns
区块链周报:数字货币发展写入十四五规划;拜登邀请MIT数字货币计划高级顾问加入总统过渡团队;委内瑞拉推出国营加密交易所
适合c/c++新手学习的一些项目,别给我错过了!
华为在5G手机市场占据绝对优势,市调机构对小米的市占出现分歧