The learning of daemon threads

Source: Internet
Author: User

These two days again Netty House saw Lee boss of an article, mainly involved in Nioeventloop and Guardian thread, the article finally raised a small problem, as follows:

Follow the code and monitor the thread's running state through Java VISUALVM as follows:

You can discover that the final JVM process automatically exits after all four threads in Bossgroup have died. Because the 5 user threads at this time (the main thread and the 4 threads in the nioEventLoopGroup-2) have all been terminated, only the daemon threads are left in the JVM, so the process exits.

Think: What happens if you happen to have a user request before bossgroup four threads die? The experiment code is as follows:

TimeUnit.SECONDS.sleep (ten);         = B.bind (port). sync ();         // wait for the server to listen        port shutdown // F.channel (). Closefuture (). sync ();    finally {        //  graceful exit, Release thread pool resource        TimeUnit.SECONDS.sleep (ten);                // thread breaks for 10 seconds, assuming that a user request is received during         bossgroup.shutdowngracefully ();         // workergroup.shutdowngracefully ();    }

The experimental results are as follows:

We will find that if a user request is received before all the listener threads are terminated and a work thread (NIOEVENTGROUP-3-1) is started, then the work thread will persist and the JVM process will not exit.

Reference documents:

https://mp.weixin.qq.com/s?__biz=MzAwMDIyNTAzMw==&mid=2651309700&idx=1&sn= Bc8fba96ca5d9fc8f98b89a70cfdd085&scene=0#rd

The learning of daemon threads

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.