当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Essential for back-end programmers: distributed transaction Basics
- Or talk No.19 | Facebook Dr. Tian Yuandong: black box optimization of hidden action set based on Monte Carlo tree search
- Huawei has an absolute advantage in the 5g mobile phone market, and the market share of Xiaomi is divided by the market survey organization
- 你的云服务器可以用来做什么?云服务器有什么用途?
- What can your cloud server do? What is the purpose of cloud server?
- 擅长To C的腾讯,如何借腾讯云在这几个行业云市场占有率第一?
- 模板引擎的整理归纳
- 阿里云视频云技术专家 LVS 演讲全文:《“云端一体”的智能媒体生产制作演进之路》
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- How to write a resume and project
猜你喜欢

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

python基础教程python opencv pytesseract 验证码识别的实现

阿里云视频云技术专家 LVS 演讲全文:《“云端一体”的智能媒体生产制作演进之路》

Bccoin tells you: what is the most reliable investment project at the end of the year!

谷歌开源能翻译101种语言的AI模型,只比Facebook多一种

Station B STM32 video learning

Ali! Visual computing developer's series of manuals (with internet disk link)

Flink从入门到真香(3、从集合和文件中读取数据)

On monotonous stack

Welcome to offer, grade P7, face-to-face sharing, 10000 words long text to take you through the interview process
随机推荐
CSP考试须知与各种小技巧
WLAN 直连(对等连接或 P2P)调研及iOS跨平台调研
后端程序员必备:分布式事务基础篇
重返全球第三,小米做对了什么?
Interpretation of deepmind's latest paper: the causal reasoning algorithm in discrete probability tree is proposed for the first time
适合c/c++新手学习的一些项目,别给我错过了!
优化if-else代码的八种方案
擅长To C的腾讯,如何借腾讯云在这几个行业云市场占有率第一?
AI weekly: employees are allowed to voluntarily reduce salary; company response: employees are happy and satisfied; tiger tooth HR takes employees out of the company; Sweden forbids Huawei ZTE 5g equi
新型存算一体芯片诞生,利好人工智能应用~
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
What can your cloud server do? What is the purpose of cloud server?
RestfulApi 学习笔记——父子资源(四)
【Python 1-6】Python教程之——数字
Eight ways to optimize if else code
Harbor项目高手问答及赠书活动
Rust : 性能测试criterion库
技术总监7年总结,如何进行正确的沟通?
Huawei has an absolute advantage in the 5g mobile phone market, and the market share of Xiaomi is divided by the market survey organization
一文剖析2020年最火十大物联网应用|IoT Analytics 年度重磅报告出炉!