Java Threads and Daemons

Source: Internet
Author: User

The threading mechanism of AVA has two types of threads: User thread, Daemon thread (daemon thread).

The operating system is not the concept of daemon, only daemon, but the Java language mechanism is built on the basis of the JVM, meaning that the Java platform to the bottom of the operating system to shield, 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. The only difference between the user thread thread and the daemon thread daemon is the departure of the virtual machine: If the user thread is all evacuated, then daemon thread does not have a good service, so the virtual machine will quit.

, the user can also 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 ().

Otherwise it will run out of a illegalthreadstateexception exception.  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 out, 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

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 ());

}

}

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 a program is executing is to complete the octave of all foreground threads, regardless of the status of the background thread.

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 Threads and Daemons

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.