当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Golang 系统ping程序探测存活主机(任意权限)
- Golang ICMP Protocol detects viable hosts
- Stm32uberide download and install - GPIO basic configuration operation - debug (based on CMSIS DAP debug)
- PMP考试通过心得分享
- I used Python to find out all the people who deleted my wechat and deleted them automatically
- Huawei has an absolute advantage in the 5g mobile phone market, and the market share of Xiaomi is divided by the market survey organization
- 小米、OPPO在欧洲市场继续飙涨,小米更是直逼苹果
- Rabbitmq (1) - basic introduction
- Research on WLAN direct connection (peer-to-peer connection or P2P) and cross platform research of IOS
- CSP考试须知与各种小技巧
猜你喜欢
金融领域首个开源中文BERT预训练模型,熵简科技推出FinBERT 1.0
10个常见的软件架构模式
It's worth seeing! EMR elastic low cost offline big data analysis best practice (with network disk link)
Improvement of maintenance mode of laravel8 update
Tight supply! Apple's iPhone 12 power chip capacity exposed
Drink soda, a bottle of soda water 1 yuan, two empty bottles can change a bottle of soda, give 20 yuan, how much soda can you
还不快看!对于阿里云云原生数据湖体系全解读!(附网盘链接)
Major changes in Huawei's cloud: Cloud & AI rises to Huawei's fourth largest BG with full fire
laravel8更新之速率限制改进
How to solve the difference between NAT IP and port IP
随机推荐
Q & A and book giving activities of harbor project experts
适合c/c++新手学习的一些项目,别给我错过了!
DeepMind 最新论文解读:首次提出离散概率树中的因果推理算法
I used Python to find out all the people who deleted my wechat and deleted them automatically
阿里撕下电商标签
Golang ICMP协议探测存活主机
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Workers, workers soul, draw lifelong members, become a person!
Tidb performance competition 11.02-11.06
应届生年薪35w+ !倒挂老员工,互联网大厂薪资为何越来越高?
2020-11-05
Stm32uberide download and install - GPIO basic configuration operation - debug (based on CMSIS DAP debug)
我们做了一个医疗版MNIST数据集,发现常见AutoML算法没那么好用
[Python 1-6] Python tutorial 1 -- number
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
用 Python 写出来的进度条,竟如此美妙~
Ali teaches you how to use the Internet of things platform! (Internet disk link attached)
On DSA of OpenGL
[Python 1-6] Python tutorial 1 -- number
重返全球第三,小米做对了什么?