Question of whether the finally code block is executed in the daemon (Daemon) thread in Java

Source: Internet
Author: User

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

This article should be a thinking in the Java Chinese version, about thread-daemon thread (Daemon thread) Introduction

Perhaps I understand the de is not good, I am the book for the daemon thread in the finally whether the implementation of the introduction a little confused, so I write a method test before I understand.

Take the example from the book for further clarification. First look at the original text: Do not look at my wordy, you can directly read the conclusion of the end of the article.

Import java.util.concurrent.timeunit;/** * Finally shoud is 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 is always run?") ); } }}

Operation Result:

Start Adaemon ...

If T.setdaemon (true) is in the main function, the result of the operation is as follows:

Start Adaemon ...

This shoud is always run?

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

And I actually run the result is not this, the actual is:

When T is a daemon thread, nothing is output

When T is not a daemon thread, the output

Start Adaemon ...
This shoud is always run?

After reading the book, it is understood that the finally block of code in the daemon thread class will not be executed (I doubt it, of course).

To start the test, I let the Main method end sleep 10 seconds, and see that when T is the daemon thread, finally the code block is executed.

To continue the test, I let the Main method end before sleep 1 seconds, finally the code block is also not executed (daemon thread is also sleep 1 seconds)

So I got the following conclusion:

If the Run method in the Runnable implementation class contains a finally block of code, if the thread that executes the instance is the daemon thread (daemon thread), the finally code block may not be executed, but it may also be executed,

That's what the text mentions. If the user thread is all out of operation, only the daemon thread is present, and the virtual machine exits, and the finally code block may not have a chance to execute.

Therefore, the Guardian thread generally do not go to the file, database, network and other resources to operate, so as not to release resources files too late, resulting in deadlock.

Question of whether the finally code block in the daemon (Daemon) thread in Java is executing

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.