The difference between a Java daemon thread and a user thread

Source: Internet
Author: User

public class Daemontest {public    static void Main (string[] args) {        new Workerthread (). Start ();        try {            thread.sleep (7500);        } catch (Interruptedexception e) {}        System.out.println ("Main Thread Ending");    }}class Workerthread extends Thread {public    workerthread () {        Setdaemon (true);   When false, (i.e. when it's a user thread),                //The Worker thread continues to run.                When True, (i.e. when it's a daemon thread),                //The Worker thread terminates when the main                 //thread Terminat Es.    }    public void Run () {        int count=0;        while (true) {            System.out.println ("Hello from Worker" +count++);            try {                sleep ()            } catch (Interruptedexception e) {}}        }}    

Simply understood: The daemon does not prevent the JVM from shutting down. The JVM cannot shut down when a user thread is running. When no user thread is running, there is no daemon thread, and the JVM shuts down.

Daemon Threading Application Example: Java garbage collection. When no thread is running, garbage is not generated and garbage collection does not work, and the JVM can shut down.

Daemon threads Apply background: Background threads (such as threads that can collect certain system state, send email threads, etc. that do not want to affect the JVM)


The difference between a Java daemon thread and a user thread

Related Article

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.