当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- This paper analyzes the top ten Internet of things applications in 2020!
- Get PMP certificate at 51CTO College
- Flink from introduction to Zhenxiang (10. Sink data output elasticsearch)
- AQS解析
- Rust: performance test criteria Library
- svg究竟是什么?
- Tidb performance competition 11.02-11.06
- python基础教程python opencv pytesseract 验证码识别的实现
- 10个常见的软件架构模式
- 适合c/c++新手学习的一些项目,别给我错过了!
猜你喜欢
wanxin finance
一文读懂机器学习“数据中毒”
Flink: from introduction to Zhenxiang (3. Reading data from collection and file)
Flink: from introduction to Zhenxiang (6. Flink implements UDF function - realizes more fine-grained control flow)
“他,程序猿,35岁,被劝退”:不要只懂代码,会说话,胜过10倍默默努力
Enabling education innovation and reconstruction with science and technology Huawei implements education informatization
Rabbitmq (1) - basic introduction
漫画:寻找股票买入卖出的最佳时机(整合版)
PMP心得分享
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
随机推荐
We made a medical version of the MNIST dataset, and found that the common automl algorithm is not so easy to use
一文读懂机器学习“数据中毒”
RestfulApi 学习笔记——父子资源(四)
Comics: looking for the best time to buy and sell stocks
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
The birth of a new integrated memory and computing chip is conducive to the application of artificial intelligence~
laravel8更新之维护模式改进
Improvement of rate limit for laravel8 update
Returning to the third place in the world, what did Xiaomi do right?
STM32CubeIDE下载安装-GPIO基本配置操作-Debug调试(基于CMSIS DAP Debug)
laravel8更新之速率限制改进
Essential for back-end programmers: distributed transaction Basics
wanxin finance
AQS analysis
供货紧张!苹果被曝 iPhone 12 电源芯片产能不足
wanxin finance
Introduction to mongodb foundation of distributed document storage database
10个常见的软件架构模式
阿里云视频云技术专家 LVS 演讲全文:《“云端一体”的智能媒体生产制作演进之路》
“他,程序猿,35岁,被劝退”:不要只懂代码,会说话,胜过10倍默默努力