JAVA Daemon Thread Understanding

Source: Internet
Author: User

Java threads are divided into two types: the user thread and the daemon thread. A daemon thread or process is a daemon thread or process, but the daemon thread in Java is a bit different from the daemon in Linux.

The daemon process in Linux actually refers to the process of running a service in the background, such as the crond of the Cron service, the httpd that provides the HTTP service, and the daemon thread in Java, which means that only daemon is left in the JVM instance, and the JVM exits.

We look at the differences between daemon threads and normal user threads through the following experiments

    1. Creates a daemon thread that runs a dead loop, exits after the main thread runs 5s, and the daemon thread exits.
 Public Static void Main(string[] args)throwsinterruptedexception {Thread thread =NewThread (New MyThread()); Thread.Setdaemon(true);//Set thread to daemon threadThread.Start(); Thread.Sleep( the);//5s after the main thread exits}Static classMyThreadImplementsRunnable {@Override         Public void Run() { while(true) {//thread dead loop                Try{//100ms printing once HelloThread.Sleep( -); }Catch(Interruptedexception e) {e.Printstacktrace(); } System. out.println("Hello"); }        }    }

Run Result: 5ms daemon thread exits.

hellohellohellohellohellohellohellohellohellohellohelloProcess finished with exit code 0
  1. Comment out the daemon so that the thread becomes a normal user thread

     Public Static void Main(string[] args)throwsinterruptedexception {Thread thread =NewThread (New MyThread());//Thread.setdaemon (TRUE);//normal user threadThread.Start(); Thread.Sleep( the);//5s after the main thread exits}Static classMyThreadImplementsRunnable {@Override     Public void Run() { while(true) {//thread dead loop            Try{//100ms printing once HelloThread.Sleep( -); }Catch(Interruptedexception e) {e.Printstacktrace(); } System. out.println("Hello"); }    }}

    Run Result: The thread will not exit after the main course is run, it is always dead loop

JAVA Daemon Thread Understanding

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.