Background thread (daemon thread)

Source: Internet
Author: User

There is a thread that runs in the background, and its task is to provide services to other threads, which are called "Background threads" (Daemon thread), also known as "Daemon threads."

A typical background thread is the timer "timer" thread, which is responsible for sending a fixed interval of time to other threads.

Background threads are often used for aftercare at the end of a task. In addition, background threads have a lower priority than other threads.

A generic thread is called a "user thread" compared to a background thread. If only a background thread is running in an app, the JVM exits the application.

A normal thread can be set as a background thread through Setdaemon (Boolean D). The method Isdaemon () can be used to test whether a particular thread is a background thread.

Package Com.liujl.thread;public class Daemonthread extends thread{    @Override public    void Run () {while        ( true) {            System.out.println ("Daemonthread is running ...");}    ;        public static void Main (string[] args) {        daemonthread dt=new daemonthread ();    Dt.setdaemon (true);        Dt.start ();    }    /**     * Dt.setdaemon (TRUE), set the DT thread as the daemon thread, then call Start ();     * After the DT thread executes for a period of time, it will automatically exit, because the thread dispatcher is monitoring that only     * A background thread is running, it terminates the operation of the referencing program.     *      * The reader can remove the code from line 13th and run the reference again, at which point the application will run indefinitely.     */}

  

Background thread (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.