Java Daemon Threads and non-daemon threads

Source: Internet
Author: User

The recent re-study of Java fundamentals, found that too much knowledge before a little bit, compared to the Java threading mechanism, there are two types of threads in Java: User thread, Daemon thread (the daemon thread), (PS: previously ignored).

It is estimated that the students who have studied UNIX development but have not studied Java in detail will be puzzled that there is no concept of the Guardian thread in the operating system, only the daemon is said, but the Java language mechanism is built on the JVM, which means that the Java platform shields the bottom of the operating system. So it can be in its own virtual platform to construct a favorable mechanism for themselves, and the language or platform of the designer is more or less to receive the impact of Unix thinking, and the daemon thread mechanism is the JVM such a platform, so the Guardian thread came into being.

The role of Daemon is to provide services for the operation of other threads, such as GC threads. In fact, the user thread and the daemon thread daemon is essentially no different, the only difference is in the virtual machine's departure: If the user thread is all evacuated, then daemon thread is not a good service, So the virtual machine also exits.

The daemon thread is not available inside the virtual machine, and the user can set the daemon thread by itself: public final void Setdaemon (Boolean on), but there are a few things to note:

1),Thread.setdaemon (true) must be set before Thread.Start () , or a illegalthreadstateexception exception will run out.  You cannot set a running regular thread as a daemon thread . (Note: This is a significant difference from the daemon, the daemon is created, let the process out of the original session control + let the process out of the original Process group Control + Let the process out of the original control terminal control; So the language mechanism pinned to the virtual machine is fundamentally different from the system-level language)

2), the new thread generated in the daemon thread is also daemon. (This is the essential difference: the Daemon fork () is no longer the daemon, although it copies the process-related information of the parent process, but the process of the child process is not the INIT process, the so-called Daemon is essentially "the parent process hangs, init adoption, Then file 0,1,2 are/dev/null, current directory to/")

3), not all applications can be assigned to daemon threads for service, such as read-write operations or computational logic. The virtual machine may have exited because the daemon thread has not come and is operating.

Example:

The daemon thread task to complete the file output

Import java.io.*;

Class Testrunnable implements runnable{

public void Run () {

try{

Thread.Sleep (1000);//The daemon thread is blocked for 1 seconds before running

File F=new file ("Daemon.txt");

FileOutputStream os=new FileOutputStream (f,true);

Os.write ("daemon". GetBytes ());

}

catch (IOException E1) {

E1.printstacktrace ();

}

catch (interruptedexception E2) {

E2.printstacktrace ();

}

}

}

public class testdemo2{

public static void Main (string[] args) throws Interruptedexception

{

Runnable tr=new testrunnable ();

Thread Thread=new thread (TR);

Thread.setdaemon (TRUE); Setting the daemon thread

Thread.Start (); Start of sub-process execution

}

}

Run Result: There is no "daemon" string in file Daemon.txt.

But if you put Thread.setdaemon (true); Set the daemon thread to comment out that the file Daemon.txt can be written to the daemon string

The standard for the JRE to determine whether the program is executed is that all the foreground thread octave is complete, regardless of the status of the background thread, so be sure to pay attention to this issue when using background threads.

But where does the daemon thread actually apply? For example, a servlet in a Web server that initializes a service thread in the background when the container starts, that is, the dispatch thread, is responsible for processing the HTTP request, and then each request comes up to the dispatch thread to take a worker thread out of the thread pool to handle the request for concurrency control.

Online pick of a map, easy to understand:

Java Daemon Threads and non-daemon threads

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.