Java concurrent Programming--Daemon threads (Daemon thread)

Source: Internet
Author: User

in theJavaThere are two types of threads in: User thread(User Thread), daemon Threads(Daemon Thread).

A daemon thread is a thread that provides a generic service in the background while the program is running, such as a garbage collection thread that is a competent guardian, and that thread is not an integral part of the program. Therefore, when all non-daemon threads end, the program terminates and kills all the daemon threads in the process. Conversely, the program will not terminate as long as any non-daemon threads are still running.

The only difference between the user thread and the daemon is that the virtual machine leaves: If the user thread is all out of operation, only the daemon thread is present, and the virtual machine exits.  

Converting a thread to a daemon can be done by calling thread setdaemon (True) method to achieve. There are a few things to keep in mind when using a daemon:

(1) Thread.setdaemon (true) must be in the thread.start ()

    illegalthreadstateexception exception. You cannot set a running regular thread as a daemon thread.  

(2)   in daemon The new thread generated in the thread is also daemon the.

(3) the daemon should never access an intrinsic resource, such as a file or a database, because it will be interrupted at any time, even in the middle of an operation.


Java concurrent Programming--Daemon threads (Daemon 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.