当前位置:网站首页>别再在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
边栏推荐
- 还不快看!对于阿里云云原生数据湖体系全解读!(附网盘链接)
- Or talk No.19 | Facebook Dr. Tian Yuandong: black box optimization of hidden action set based on Monte Carlo tree search
- 数据库连接报错之IO异常(The Network Adapter could not establish the connection)
- 一文剖析2020年最火十大物联网应用|IoT Analytics 年度重磅报告出炉!
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- The first open source Chinese Bert pre training model in the financial field
- Returning to the third place in the world, what did Xiaomi do right?
- 我用 Python 找出了删除我微信的所有人并将他们自动化删除了
- “他,程序猿,35岁,被劝退”:不要只懂代码,会说话,胜过10倍默默努力
- wanxin finance
猜你喜欢
Share the experience of passing the PMP examination
python基础教程python opencv pytesseract 验证码识别的实现
Introduction to mongodb foundation of distributed document storage database
Improvement of maintenance mode of laravel8 update
Flink: from introduction to Zhenxiang (6. Flink implements UDF function - realizes more fine-grained control flow)
python基本语法 变量
三、函数的参数
Major changes in Huawei's cloud: Cloud & AI rises to Huawei's fourth largest BG with full fire
Interpretation of deepmind's latest paper: the causal reasoning algorithm in discrete probability tree is proposed for the first time
What is the database paradigm
随机推荐
Android Basics - check box
Tight supply! Apple's iPhone 12 power chip capacity exposed
Ubuntu20.04 access FTP server garbled problem + upload files
最全!阿里巴巴经济体云原生实践!(附网盘链接)
我们做了一个医疗版MNIST数据集,发现常见AutoML算法没那么好用
From a friend recently Ali, Tencent, meituan and other P7 Python development post interview questions
用 Python 写出来的进度条,竟如此美妙~
区块链周报:数字货币发展写入十四五规划;拜登邀请MIT数字货币计划高级顾问加入总统过渡团队;委内瑞拉推出国营加密交易所
On DSA of OpenGL
阿里云视频云技术专家 LVS 演讲全文:《“云端一体”的智能媒体生产制作演进之路》
nat转换的ip跟端口ip不相同的解决方法
This paper analyzes the top ten Internet of things applications in 2020!
What is the database paradigm
后端程序员必备:分布式事务基础篇
10 common software architecture patterns
Rabbitmq (1) - basic introduction
DeepMind 最新论文解读:首次提出离散概率树中的因果推理算法
供货紧张!苹果被曝 iPhone 12 电源芯片产能不足
Suitable for C / C + + novice learning some projects, do not give me to miss!
RestfulApi 学习笔记——父子资源(四)