当前位置:网站首页>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:15 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]所创,转载请带上原文链接,感谢