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