Whether the finally code block in the Daemon thread in Java is executed, daemonfinally

Source: Internet
Author: User

Whether the finally code block in the Daemon thread in Java is executed, daemonfinally

Http://www.cnblogs.com/luochengor/archive/2011/08/11/2134818.html

This article should be a section of Thinking in Java Chinese version, about Thread-Daemon Thread

 

It may be because I don't understand de well. I am a little confused about whether the finally in the daemon thread is executed, so I can understand it after I write a method test.

Take the example in the book for further explanation. Let's take a look at the original article: If you don't look at me, you can directly read the conclusion at the end of the article.

import java.util.concurrent.TimeUnit;/** * Finally shoud be always run ? */public class DaemonsDontRunFinally {    public static void main(String[] args) {        Thread t = new Thread(new ADaemon());        t.setDaemon(true);        t.start();    }}class ADaemon implements Runnable {    public void run() {        try {            System.out.println("start ADaemon...");            TimeUnit.SECONDS.sleep(1);        } catch (InterruptedException e) {            System.out.println("Exiting via InterruptedException");        } finally {            System.out.println("This shoud be always run ?");        }    }}

Running result:

Start ADaemon...

If you set t. setDaemon (True); Comment out, and the running result is as follows:

Start ADaemon...

This shoud be always run?

-----------------------------------------------------------------------

The actual running result is not as follows:

T is a daemon thread, and nothing is output.

T is output when it is not a daemon thread

Start ADaemon...
This shoud be always run?

After reading the description in the book, I understand that the finally code block in the daemon thread class will not be executed (of course I doubt this)

Start the test. I asked the main method to sleep for 10 seconds before the end. When t is a daemon thread, the finally code block is executed.

Continue the test. I asked the main method to finish sleep for 1 second, And the finally code block was not executed (the daemon thread was also sleep for 1 second)

So I got the following conclusion:

If the run method in the runnable implementation class contains a finally code block, if the thread executing the instance is a daemon thread, the finally code block may not be executed, but it may also be executed,

That is to say, if all user threads have exited and only the daemon thread exists, the virtual machine will quit, and the finally code block may not be executed.

Therefore, do not operate on files, databases, networks, and other resources in the daemon thread to prevent resource files from being released too late, resulting in deadlocks.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.