当前位置:网站首页>别再在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
边栏推荐
- Windows下快递投递柜、寄存柜的软件初探
- 浅谈OpenGL之DSA
- [Python 1-6] Python tutorial 1 -- number
- Rust : 性能测试criterion库
- Get PMP certificate at 51CTO College
- 一文读懂机器学习“数据中毒”
- I used Python to find out all the people who deleted my wechat and deleted them automatically
- 新型存算一体芯片诞生,利好人工智能应用~
- Stm32uberide download and install - GPIO basic configuration operation - debug (based on CMSIS DAP debug)
- From a friend recently Ali, Tencent, meituan and other P7 Python development post interview questions
猜你喜欢

当Kubernetes遇到机密计算,看阿里巴巴如何保护容器内数据的安全!(附网盘链接)

Xamarin deploys IOS from scratch Walterlv.CloudKeyboard application

This year's salary is 35W +! Why is the salary of Internet companies getting higher and higher?

2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...

laravel8更新之速率限制改进

The most complete! Alibaba economy cloud original practice! (Internet disk link attached)

吐血整理!阿里巴巴 Android 开发手册!(附网盘链接)

10个常见的软件架构模式

What can your cloud server do? What is the purpose of cloud server?

How to write a resume and project
随机推荐
Xamarin deploys IOS from scratch Walterlv.CloudKeyboard application
Returning to the third place in the world, what did Xiaomi do right?
Improvement of maintenance mode of laravel8 update
I used Python to find out all the people who deleted my wechat and deleted them automatically
阿里教你深入浅出玩转物联网平台!(附网盘链接)
供货紧张!苹果被曝 iPhone 12 电源芯片产能不足
模板引擎的整理归纳
Comics: looking for the best time to buy and sell stocks
“他,程序猿,35岁,被劝退”:不要只懂代码,会说话,胜过10倍默默努力
Rust : 性能测试criterion库
Win10 terminal + WSL 2 installation and configuration guide, exquisite development experience
laravel8更新之维护模式改进
打工人,打工魂,抽终身会员,成为人上人!
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
【Python 1-6】Python教程之——数字
The most complete! Alibaba economy cloud original practice! (Internet disk link attached)
DeepMind 最新论文解读:首次提出离散概率树中的因果推理算法
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
漫画|讲解一下如何写简历&项目
Android Basics - check box