In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand java highly concurrent user threads and daemon threads". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand java highly concurrent user threads and daemon threads".
Daemon thread is a special thread, which silently completes some systematic services in the background, such as garbage collection thread and JIT thread. Corresponding to the user thread, the user thread can be understood as the worker thread of the system, which will complete the business operations that the program needs to complete. If the user thread is all finished, it means that the business operation that the program needs to complete is over, and the system can exit. So when there are only daemons left in the system, the java virtual machine automatically exits.
Java threads are divided into user threads and daemon threads. The daemon attribute of a thread is true, which indicates that it is a daemon thread, and false indicates that it is a user thread.
Let's take a look at some of the features of daemon threads.
When there is only a daemon thread in the program, the system will automatically exit package com.itsoku.chat03; / * Wechat official account: passerby A Java, focusing on java technology sharing (taking you to play with crawlers, distributed transactions, asynchronous messaging services, task scheduling, sub-database sub-tables, big data, etc.), please follow! * / public class Demo1 {public static class T1 extends Thread {public T1 (String name) {super (name);} @ Override public void run () {System.out.println (this.getName () + "start execution" + (this.isDaemon ()? "I am the guardian thread": "I am the user thread"); while (true);}} public static void main (String [] args) {T1 = new T1 ("child thread 1"); t1.start (); System.out.println ("main thread ends");}}
Run the above code, and the result is as follows:
You can see that the main thread has ended, but the program cannot exit because child thread 1 is a user thread with an endless loop that has been running and cannot be finished.
Take a look at the following code:
Package com.itsoku.chat03; / * Wechat official account: passers-by A java, specializing in Java technology sharing (take you to play with crawlers, distributed transactions, asynchronous messaging services, task scheduling, sub-database sub-table, big data, etc.), please follow! * / public class Demo2 {public static class T1 extends Thread {public T1 (String name) {super (name);} @ Override public void run () {System.out.println (this.getName () + "start execution" + (this.isDaemon ()? "I am the guardian thread": "I am the user thread"); while (true);}} public static void main (String [] args) {T1 = new T1 ("child thread 1"); t1.setDaemon (true); t1.start () System.out.println ("main thread ends");}}
Running result:
The program can end normally, through t1.setDaemon (true) in the code; the T1 thread is set as the daemon thread, and after the main thread where the main method is executed, the program exits.
Conclusion: when all the user threads in the program are finished, the system will exit automatically regardless of whether the daemon thread ends or not.
To set up a daemon thread, you need to package com.itsoku.chat03; import java.util.concurrent.TimeUnit; * Wechat official account before the start () method: passerby A Java, focusing on java technology sharing (taking you to play with crawlers, distributed transactions, asynchronous messaging services, task scheduling, sub-database sub-tables, big data, etc.), please follow! * / public class Demo3 {public static void main (String [] args) {Thread T1 = new Thread () {@ Override public void run () {try {TimeUnit.SECONDS.sleep (10) } catch (InterruptedException e) {e.printStackTrace ();}; t1.start (); t1.setDaemon (true);}}
T1.setDaemon (true); is executed after the start () method of T1, and the execution reports an exception. The running result is as follows:
Default value for thread daemon
Let's take a look at the thread creation source code, which is in the init () method of the Thread class:
Thread parent = currentThread (); this.daemon = parent.isDaemon ()
The default value of dameon is the daemon of the parent thread, that is, if the parent thread is the user thread, the child thread is also the user site by default, and if the parent thread is a daemon thread, the child thread is also the daemon thread by default.
Sample code:
Package com.itsoku.chat03; import java.util.concurrent.TimeUnit; * Wechat official account: passers-by A java, specializing in Java technology sharing (take you to play with crawlers, distributed transactions, asynchronous messaging services, task scheduling, sub-database sub-table, big data, etc.), please follow! * / public class Demo4 {public static class T1 extends Thread {public T1 (String name) {super (name);} @ Override public void run () {System.out.println (this.getName () + ".daemon:" + this.isDaemon ()) } public static void main (String [] args) throws InterruptedException {System.out.println (Thread.currentThread (). GetName () + ".daemon:" + Thread.currentThread () .isDaemon ()); T1 T1 = new T1 ("T1"); t1.start () Thread T2 = new Thread () {@ Override public void run () {System.out.println (this.getName () + ".daemon:" + this.isDaemon ()); T1 T3 = new T1 ("T3"); t3.start () }; t2.setName ("T2"); t2.setDaemon (true); t2.start (); TimeUnit.SECONDS.sleep (2);}}
Run the code and output:
Main.daemon:false
T1.daemon:false
T2.daemon:true
T3.daemon:true
T1 is created by the main thread (the thread where the main method resides), and the main thread is the parent thread of T1, so t1.daemon is false, indicating that T1 is the user thread.
The T2 thread calls setDaemon (true); it is set as the daemon thread, and T3 is created by T2, so the default thread type of T3 is the same as T2, and t2.daemon is true.
Thank you for your reading, the above is the content of "how to understand java highly concurrent user threads and daemon threads". After the study of this article, I believe you have a deeper understanding of how to understand java highly concurrent user threads and daemon threads, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.