当前位置:网站首页>别再在finally里面释放资源了,解锁个新姿势!
别再在finally里面释放资源了,解锁个新姿势!
2020-11-08 13:47:00 【osc_zq2o0u6t】
版权说明: 本文由博主原创,转载请注明出处。
原文地址: https://blog.csdn.net/qq_38688267/article/details/109511716
在我们编码过程中,不可避免的会用到于文件操作 IO 流、数据库连接等开销比较大的资源,用完之后需要通过 close 方法将其关闭,否则资源一直处于打开状态,可能会导致内存泄露等问题。
拿文件操作流举例,我们在使用时要try catch
,用完了在finally
中关闭,而关闭的时候还需要再try catch
,可以说是非常麻烦了!代码如下:
/**传统写法**/
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
}
}
}
而我们的新姿势是使用JDK1.7中的try-with-resources
语法,直接上代码:
/**使用语法糖**/
try (BufferedReader br1 = new BufferedReader(new FileReader(""))) {
String line;
while ((line = br1.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
// DO something
}
代码是不是瞬间清爽了很多?赶紧用起来吧~
我们顺便再来刨根究底一下吧,看下他编译之后的样子:
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) {
}
其实背后的原理也很简单,让编译器都帮我们做了关闭资源的工作而已。所以,再次印证了,语法糖的作用就是方便程序员的使用,最终还是要转成编译器认识的语言。
希望本文对大家有所帮助或启发。码字不易,觉得写的不错的可以点赞支持一下哦~
版权声明
本文为[osc_zq2o0u6t]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4360121/blog/4708185
边栏推荐
- 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
- 【Python 1-6】Python教程之——数字
- 来自朋友最近阿里、腾讯、美团等P7级Python开发岗位面试题
- Get PMP certificate at 51CTO College
- Android Basics - check box
- Rust: performance test criteria Library
- 浅谈OpenGL之DSA
- 重返全球第三,小米做对了什么?
- laravel8更新之维护模式改进
- 一文剖析2020年最火十大物联网应用|IoT Analytics 年度重磅报告出炉!
猜你喜欢
Tidb performance competition 11.02-11.06
Service architecture and transformation optimization process of e-commerce trading platform in mogujie (including ppt)
Where is the new target market? What is the anchored product? |Ten questions 2021 Chinese enterprise service
Essential for back-end programmers: distributed transaction Basics
原创 | 数据资产确权浅议
2035 we will build such a country
Stm32uberide download and install - GPIO basic configuration operation - debug (based on CMSIS DAP debug)
最全!阿里巴巴经济体云原生实践!(附网盘链接)
python基础教程python opencv pytesseract 验证码识别的实现
rabbitmq(一)-基础入门
随机推荐
wanxin finance
STM32CubeIDE下载安装-GPIO基本配置操作-Debug调试(基于CMSIS DAP Debug)
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
On the confirmation of original data assets
BCCOIN告诉您:年底最靠谱的投资项目是什么!
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
“他,程序猿,35岁,被劝退”:不要只懂代码,会说话,胜过10倍默默努力
最全!阿里巴巴经济体云原生实践!(附网盘链接)
Share the experience of passing the PMP examination
谷歌开源能翻译101种语言的AI模型,只比Facebook多一种
供货紧张!苹果被曝 iPhone 12 电源芯片产能不足
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Ali! Visual computing developer's series of manuals (with internet disk link)
【Python 1-6】Python教程之——数字
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Rust: performance test criteria Library
laravel8更新之速率限制改进
It's worth seeing! EMR elastic low cost offline big data analysis best practice (with network disk link)
阿里云加速增长,进一步巩固领先优势
Interpretation of deepmind's latest paper: the causal reasoning algorithm in discrete probability tree is proposed for the first time