In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article analyzes "how to analyze and use daemon threads in Java". The content is detailed and easy to understand. Friends who are interested in "how to analyze and use the guardian thread in Java" can follow the editor's train of thought to read it slowly and deeply. I hope it will be helpful to everyone after reading. Let's follow the editor to learn more about how to analyze and use daemon threads in Java.
What is Java? Java is an object-oriented programming language that can write desktop applications, Web applications, distributed systems, and embedded system applications.
What is a daemon thread
Before we talk about daemon threads, let's talk about what user threads are.
User thread: the ordinary thread that we usually create.
Daemon thread (that is, Daemon thread): it is a service thread that serves the user thread; there is no need for upper-level logic intervention, but of course we can create a daemon thread manually. In JVM, after all non-daemon threads are executed, the virtual machine automatically exits with or without daemon threads.
Second, why do you need a daemon thread
JVM does not exit when there is any user thread. So under what circumstances can the JVM program exit normally?
The Java Virtual Machine exits when the only threads running are all daemon threads.
The above sentence comes from the official JDK document, which means:
When there is no running non-daemon thread in the JVM, the JVM process exits.
First of all, it is important to understand that when there is any user thread, JVM will not exit.
If the user thread does not end, the JVM process cannot end. This leads to a question: who is responsible for ending the thread? So java provides daemon threads (Daemon Thread) to help us solve this problem.
Third, how to use it
It's easy to use, but before calling the start () method, call setDaemon (true) to mark the thread as a daemon thread.
How to check whether a thread is a daemon thread or a user thread: use the isDaemon () method.
From the output of the console, we can see that when the main thread exits, the JVM will exit and the daemon thread will be recycled, even if you have a dead loop inside.
Note:
Thread.setDaemon (true) must be set before thread.start (), or an IllegalThreadStateException exception will be thrown. The new thread generated in the Daemon thread is also Daemon. Fourth, the function and application scenario of daemon thread
As we already know above, if there is not a running non-daemon thread in the JVM, JVM exits at this point.
The garbage collection thread in JVM is a typical daemon thread. If you don't have this feature, when JVM exits, the program cannot quit because the garbage collection thread is still running. This shows the importance of daemon threads.
Garbage collection is a mechanism that needs to be run all the time, but when there is no user thread, there is no need for a garbage collection thread, and the daemon thread just meets this need.
Generally speaking, daemon threads are often used to perform background tasks, but you want the thread to shut down automatically when the program exits, or JVM exits, so daemon threads are your first choice.
This is the end of the analysis and use of the daemon thread in Java. I hope the above content can improve everyone. If you want to learn more knowledge, please pay more attention to the editor's updates. Thank you for following the website!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.