Summary of cases where final blocks of code are not executed

Keywords: Java Programming

Before discussing final keywords with others, I always said simply: "fianly code blocks in the code will be executed, generally used to clear IO resources and other non-memory resources (memory resources recycled by GC mechanism)." Today, I found several situations that will not be implemented. Now I will summarize them.

1. The Meaning of finally

The real meaning of final is that only when you come out of the try block, you must execute the corresponding final block.

public class Test {
    public static void main(String[] args) {
        foo(false);
    }
    public static void foo(boolean flag) {
        System.out.println("enter foo()");
        if(flag) {
          try {
              System.out.println("enter try block");
          } finally {
              System.out.println("enter finally block");
          }
        } else {
            System.out.println("leave foo()");
        }

    }
}
/******************
The console prints as follows
enter foo()
leave foo()
*******************/

The above code, flag is false, did not enter the try code block, the corresponding final will naturally not execute.

2.System.exit()

The function of System.exit() is to stop the current virtual machine. The virtual machine is aborted and the final block of code will not execute naturally.

public class Test {
    public static void main(String[] args) {
        foo();
    }
    public static void foo() {
        System.out.println("enter foo()");
       try {
            System.out.println("enter try block");
            System.exit();
       } finally {
            System.out.println("enter finally block");
       }
    }
}
/*****************
The console prints as follows
enter foo()
enter try block
******************/

 

The above code goes into the foo() method and then into the try code block, but the System.exit() is called to abort the virtual machine before entering the final code block, and the final code block will not be executed.

3. When daemon threads are aborted

java threads are divided into two categories, daemon threads and non-daemon threads. When all non-daemon threads abort, regardless of the existence of daemon threads, the virtual machine will kill the daemon threads and terminate the program. In a virtual machine, the thread executing main method is a non-daemon thread, while garbage collection is another daemon thread. When main is finished, the program terminates, regardless of whether the garbage collection thread terminates or not. Therefore, if a final block of code exists in the daemon thread, then when all non-daemon threads abort, the daemon thread is killed, and its final block of code will not execute.

public class Test {
    public static void main(String[] args) {
        Thread t = new Thread(new Task());
        t.setDaemon(true); //Set it as a daemon thread
        t.start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException("the "+Thread.currentThread().getName()+" has been interrupted",e);
        }
    }
}
class Task implements Runnable {
    @Override
    public void run() {
         System.out.println("enter run()");
        try {
            System.out.println("enter try block");
            TimeUnit.SECONDS.sleep(5); //Block 5 s
        } catch(InterruptedException e) {
            System.out.println("enter catch block");
            throw new RuntimeException("the "+Thread.currentThread().getName()+" has been interrupted",e);
        } finally {
            System.out.println("enter finally block");
        }      
    }
}
/*******************
The console prints as follows
enter run()
enter try block
********************/

After the main() is executed, the non-daemon thread ends. Although the thread t is blocked, the program will still stop because it is a daemon thread. Moreover, even if it enters the try block, the final block will not be executed.

summary

The final block of code is not necessarily executed, and it will not execute without entering the try block or when the program is aborted.

Quote

1. Java Programming Thought

Posted by Cardale on Wed, 10 Apr 2019 18:06:30 -0700