当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Tips and skills of CSP examination
- 2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
- 2035我们将建成这样的国家
- Golang 系统ping程序探测存活主机(任意权限)
- AQS解析
- Interpretation of deepmind's latest paper: the causal reasoning algorithm in discrete probability tree is proposed for the first time
- Python basic syntax
- Summary of template engine
- 2035 we will build such a country
- Q & A and book giving activities of harbor project experts
猜你喜欢
I used Python to find out all the people who deleted my wechat and deleted them automatically
Introduction to mongodb foundation of distributed document storage database
Ali teaches you how to use the Internet of things platform! (Internet disk link attached)
PMP心得分享
The most complete! Alibaba economy cloud original practice! (Internet disk link attached)
我们做了一个医疗版MNIST数据集,发现常见AutoML算法没那么好用
刚刚好,才是最理想的状态
This time Kwai tiktok is faster than shaking.
Q & A and book giving activities of harbor project experts
新型存算一体芯片诞生,利好人工智能应用~
随机推荐
Flink from introduction to Zhenxiang (10. Sink data output elasticsearch)
Powershell 使用.Net对象发送邮件
Tidb performance competition 11.02-11.06
软件开发中如何与人协作? | 每日趣闻
Flink从入门到真香(6、Flink实现UDF函数-实现更细粒度的控制流)
Rust: performance test criteria Library
这次,快手终于比抖音'快'了!
2035 we will build such a country
喝汽水,1瓶汽水1元,2个空瓶可以换一瓶汽水,给20元,可以多少汽水
OR Talk NO.19 | Facebook田渊栋博士:基于蒙特卡洛树搜索的隐动作集黑盒优化 - 知乎
Summary of template engine
DeepMind 最新论文解读:首次提出离散概率树中的因果推理算法
We made a medical version of the MNIST dataset, and found that the common automl algorithm is not so easy to use
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
最全!阿里巴巴经济体云原生实践!(附网盘链接)
Research on WLAN direct connection (peer-to-peer connection or P2P) and cross platform research of IOS
模板引擎的整理归纳
一文读懂机器学习“数据中毒”
擅长To C的腾讯,如何借腾讯云在这几个行业云市场占有率第一?
来自朋友最近阿里、腾讯、美团等P7级Python开发岗位面试题