Daemon and java daemon

Source: Internet
Author: User

Daemon processes are the background service processes in Linux. It is a long-lived process, usually independent of the control terminal and periodically executes a task or waits to process some events. Daemon is often started during system boot and ended when the system is shut down. In Linux, there are many daemon processes. Most services are implemented through daemon. At the same time, daemon can complete many system tasks, for example, job planning process crond and printing process lqd (the ending letter d indicates Daemon ). The Daemon thread automatically leaves when no user thread is available for service. In Java, a special thread is a low-level thread called Daemon. This thread has the lowest priority and is used to provide services for other objects and threads in the system. To set a user thread as a daemon, call the setDaemon method of the thread object before the thread object is created. A typical example of a daemon thread is the automatic collection of system resources in the JVM. the Java garbage collection thread we are familiar with is a typical daemon thread, when there is no running Thread in our program, the program will no longer produce garbage, and the garbage collector will have nothing to do. So when the garbage collection Thread is the only Thread left on the Java virtual machine, the Java virtual machine automatically leaves. It is always running in a low-level state for real-time monitoring and management of recoverable resources in the system. Daemon is a special process running in the background. It is independent of the control terminal and periodically executes a task or waits to process some events. That is to say, the daemon thread does not depend on the terminal, but on the system. It is "same to the system ". What does the Java daemon look like. When all the threads in the JVM are daemon threads, the JVM can exit. If there is one or more non-daemon threads, the JVM will not exit.

Public class DameonThread extends Thread {public DameonThread () {}/ ***** // *** Thread's run method, which will run with other threads at the same time */public void run () {for (int I = 1; I <= 100; I ++) {try {Thread. sleep (100);} catch (InterruptedException ex) {ex. printStackTrace ();} System. out. println (I) ;}} public static void main (String [] args) {DameonThread test = new DameonThread (); test. setDaemon (true); test. start (); System. out. println ("isDaemon =" + test. isDaemon (); try {System. in. read (); // accept the input so that the program pauses here. Once the user input is received, the main thread ends and the daemon thread ends automatically} catch (IOException ex) {ex. printStackTrace ();}}}

 

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.