Background thread (daemon thread) and background thread daemon

Source: Internet
Author: User

Background thread (daemon thread) and background thread daemon

   

There is a Thread that runs in the background, and its tasks provide services for other threads. Such threads are called "Daemon Threads ), also known as "daemon thread ".

   

A typical background thread is a Timer "Timer" thread that sends a fixed interval to other threads.

   

Background threads are often used to handle the aftermath of a task. In addition, the background thread has a lower priority than other threads.

   

Compared with the background thread, a general thread is called a "User thread ". If only the background thread is running in an application, JVM will exit the application.

You can use setDaemon (boolean d) to set a common thread as a background thread. IsDaemon () can be used to test whether a specific 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): If the dt thread is set as the daemthread, start () is called. * after the dt thread is executed for a period of time, it will automatically exit, this is because when the thread scheduler detects that only * a background thread is running, it terminates the running of the referenced program. ** The reader can remove the 13th lines of code and run the reference again. At this time, the application will run continuously. */}

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.