高效 告别996,开启java高效编程之门 4-5TWR进阶与特殊情况

1    重点:

1.1  TWR简介

1.2  class反编译查看TWR方式关闭物理流资源

 

 

2    TWR简介

◆Java7引入新特性
◆优雅关闭资源
◆一种Java语法糖

 

 

 

3    class反编译查看TWR方式关闭物理流资源(idea内置工具class反编译)

 

 

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.imooc.zhangxiaoxi.resource;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;

public class FileCopyTestNew {
    public FileCopyTestNew() {
    }

    @Test
    public void fileCopyTestNew() {
        String fileInputStreamUrl = "lib2/FileCopyTest.java";
        String fileOutputStreamUrl = "targetTest/target2.txt";

        try {
            FileInputStream fileInputStream = new FileInputStream(fileInputStreamUrl);
            Throwable var4 = null;

            try {
                FileOutputStream fileOutputStream = new FileOutputStream(fileOutputStreamUrl);
                Throwable var6 = null;

                try {
                    int conent;
                    try {
                        while((conent = fileInputStream.read()) != -1) {
                            fileOutputStream.write(conent);
                        }
                    } catch (Throwable var22) {
                        var6 = var22;
                        throw var22;
                    }
                } finally {
                    $closeResource(var6, fileOutputStream);
                }
            } catch (Throwable var24) {
                var4 = var24;
                throw var24;
            } finally {
                $closeResource(var4, fileInputStream);
            }
        } catch (FileNotFoundException var26) {
            var26.printStackTrace();
        } catch (IOException var27) {
            var27.printStackTrace();
        }

    }
}

 

 

 

 

 

 

4    课程缺失

 

 

 

5    资源关闭特殊情况

◆资源对象被return的情况下,由调用方关闭
◆ByteArraylnputStream等不需要检查关闭的资源对象,
◆使用Socket获取的InputStream和OutputStream对象不需要关闭

 

上一篇:震惊!薛兆丰居然这样看待996?


下一篇:Oracle解决用户锁的问题