最后用java中的try方法

bim*_*wla 1 java

类DaemonThread扩展Thread {

public void run() {
    System.out.println("Entering run method");

    try {
        System.out.println("In run Method: currentThread() is"
            + Thread.currentThread());

        while (true) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException x) {
                System.out.println("hi");
            }

            // System.out.println("In run method: woke up again");

            finally {
                System.out.println("Leaving run1 Method");
            }
        }
    } finally {
        System.out.println("Leaving run Method");
    }

}

public static void main(String[] args) {
    System.out.println("Entering main Method");

    DaemonThread t = new DaemonThread();
    t.setDaemon(true);
    t.start();

    try {
        Thread.sleep(900);
    } catch (InterruptedException x) {}

    System.out.println("Leaving main method");
}

}
Run Code Online (Sandbox Code Playgroud)

为什么第二个终于方法没有运行...因为我知道最后方法必须要运行任何条件是..但在这种情况下只有第一个最终方法,为什么不第二个最终运行.

Jea*_*art 6

println声明是从来没有达到,因为的while(true),永远不会结束循环!

如果您离开该循环,则将执行第二个finally块.