Daemon Threads and non-daemon threads

Source: Internet
Author: User

What is a daemon thread and what is a non-daemon thread

User threads: non-daemon threads include regular user threads or event dispatch threads such as those used to handle GUI events, and the Java virtual machine automatically leaves after all its non-daemon threads have left.

Daemon Thread: The daemon thread is used to serve the user thread, such as a GC thread. If no other user thread is running, there is no service object, and there is no reason to go on.

the only difference between a daemon thread and a user thread is that, in fact, the UI thread and the daemon thread daemon are fundamentally different, and the only difference is that the virtual machine leaves, and when all the threads in the JVM are daemons, The JVM can exit (if the user thread is fully evacuated, then the daemon thread does not have a thread to serve, so the virtual machine exits); if there is one or more non-daemons, it will not exit.

1, Thread.setdaemon (true) must be set before Thread.Start (), you cannot set the running regular thread as the daemon thread, otherwise you will run out a illegalthreadstateexception exception, The Isdaemon method returns True if the thread is a daemon thread

2. Not all applications can be assigned to daemon threads for service, such as read-write operations or computational logic. The virtual machine may have exited because the daemon thread has not come and is operating.

There is no difference between the daemon thread and the user thread, which can be thought of as serving the user thread, not performing business-related computational logic and IO operations, because the JVM exits when the user thread is executed, and some daemons do not.

Before Thread.Start (), set Setdeamon (true) to

Daemon Threads and non-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.