Daemon thread and normal thread

Source: Internet
Author: User

The only difference between a daemon and a normal thread is that the JVM exits when all the threads in the JVM are daemons, andif one or more non-daemons do not exit. (The above is for normal exit, call System.exit will definitely exit)

So the only meaning of Setdeamon (true) is to tell the JVM that it doesn't have to wait for it to exit, and let the JVM quit if it likes to quit.

The

Daemon thread automatically leaves without a user thread to serve, and a special thread in Java is a low-level thread called a daemon (Daemon) thread. This thread has the lowest priority for serving other objects and threads in the system. The way to set a user thread as a daemon is to invoke the Setdaemon method of the thread object before it is created. A typical example of a daemon thread is a system resource recycle thread in the JVM, and the Java garbage collection thread we are familiar with is a typical daemon thread, and when no more threads are running in our program, the program will no longer generate garbage, and the garbage collector will have nothing to do. So when the garbage collection thread is the only thread left on the Java virtual machine, the Java virtual opportunity automatically leaves. It is always running in a low-level state for real-time monitoring and management of recyclable resources in the system. A daemon (Daemon) is a special process that runs in the background. It is independent of the control terminal and periodically performs some sort of task or waits to handle certain occurrences. That is, the daemon thread does not depend on the terminal, but relies on the system, "die" with the system. What is the Java daemon thread like? The JVM exits when all the threads in the JVM are daemons, and the JVM does not exit if there is one or more non-daemon threads.

Java has two threads: "Daemon thread daemon" and "User thread users". The example we saw earlier is the user, the daemon is a thread that "provides universal support in the background" and is not part of the program ontology. It is literally easy to think of a daemon thread as being created internally by a virtual machine, while a user thread is created by itself. This is not the case, and any thread can be either "Daemon thread daemon" or "User thread users". They are identical in almost every respect, and the only difference is when the virtual machine leaves: The user thread: the Java Virtual machine automatically leaves after all its non-daemon threads have left. Daemon Thread: The daemon is used to serve the user thread, and if no other user thread is running, there is no service object and there is no reason to go on. The Setdaemon (Boolean on) method makes it easy to set the thread's daemon mode, true to daemon mode, and false to user mode. The Setdaemon (Boolean on) method must be called before the thread is started, and an exception is generated when threads are running. The Isdaemon method tests whether the thread is a daemon thread. It is worth mentioning that when you create other threads in a daemon thread, these newly generated threads do not have to set the Daemon property, they will be the daemon thread, and the user thread is the same.
Package Ying.thread;import Java.io.ioexception;public class Test extends Thread {public    Test () {             }         Public  void Run () {for         (int i = 0; i <; i + +) {             try {                thread.sleep]            } catch (interruptedexception e) {                //TODO auto-generated catch block                e.printstacktrace ();            }             System.out.println (i);           }    }                 public static void Main (String args[]) {         test test = new test ();         Test.setdaemon (true);         Test.start ();         System.out.println ("isdaemon=" + Test.isdaemon ());         try {            System.in.read ();        } catch (IOException e) {            //TODO auto-generated catch block            E.printstacktrace ();}}}     
If there is no user thread, then the daemon thread does not have the meaning to survive:
Package Ying.thread;import Java.io.ioexception;public class Test extends Thread {public    Test () {             }         Public  void Run () {for         (int i = 0; i <; i + +) {             try {                thread.sleep]            } catch (interruptedexception e) {                //TODO auto-generated catch block                e.printstacktrace ();            }             System.out.println (i);           }    }                 public static void Main (String args[]) {         test test = new test ();         Test.setdaemon (true);         Test.start ();         System.out.println ("isdaemon=" + Test.isdaemon ());}     }
Nothing is done after this program executes, nothing is printed; if we put Thread.setdaemon (true), then we can print out the numbers. This is the daemon thread, guarding the last user thread, and if there is no user thread, he has no effect. No Exit wait what??

Daemon thread and normal thread

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.