当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 漫画:寻找股票买入卖出的最佳时机(整合版)
- I used Python to find out all the people who deleted my wechat and deleted them automatically
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- 技术总监7年总结,如何进行正确的沟通?
- 喜获蚂蚁offer,定级p7,面经分享,万字长文带你走完面试全过程
- PMP考试通过心得分享
- 2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
- Interpretation of deepmind's latest paper: the causal reasoning algorithm in discrete probability tree is proposed for the first time
- Entry level! Teach you how to develop small programs without asking for help (with internet disk link)
- 区块链周报:数字货币发展写入十四五规划;拜登邀请MIT数字货币计划高级顾问加入总统过渡团队;委内瑞拉推出国营加密交易所
猜你喜欢

数据库连接报错之IO异常(The Network Adapter could not establish the connection)

2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...

小米、OPPO在欧洲市场继续飙涨,小米更是直逼苹果

后端程序员必备:分布式事务基础篇

分布式文档存储数据库之MongoDB基础入门

Tips and skills of CSP examination

重返全球第三,小米做对了什么?

适合c/c++新手学习的一些项目,别给我错过了!

Or talk No.19 | Facebook Dr. Tian Yuandong: black box optimization of hidden action set based on Monte Carlo tree search

BCCOIN告诉您:年底最靠谱的投资项目是什么!
随机推荐
浅谈OpenGL之DSA
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
This year's salary is 35W +! Why is the salary of Internet companies getting higher and higher?
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Huawei has an absolute advantage in the 5g mobile phone market, and the market share of Xiaomi is divided by the market survey organization
还不快看!对于阿里云云原生数据湖体系全解读!(附网盘链接)
Golang ICMP协议探测存活主机
STM32CubeIDE下载安装-GPIO基本配置操作-Debug调试(基于CMSIS DAP Debug)
svg究竟是什么?
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
【Python 1-6】Python教程之——数字
The network adapter could not establish the connection
Harbor项目高手问答及赠书活动
用科技赋能教育创新与重构 华为将教育信息化落到实处
三、函数的参数
Ubuntu20.04 access FTP server garbled problem + upload files
B站stm32视频学习
华为云重大变革:Cloud&AI 升至华为第四大 BG ,火力全开
Golang system ping program to detect the surviving host (any permission)