Java daemon and non-daemon threads

Source: Internet
Author: User


Https://www.cnblogs.com/super-d2/p/3348183.html

A recent review of Java Basics has found that too much knowledge has been taken before, compared to Java's threading mechanism, in Java there are two types of threads: User thread (Users threads), Daemon thread (daemon), (PS: previously ignored).

It is estimated that the students who have studied the UNIX development but did not study Java carefully will be puzzled, the operating system is not called the concept of a daemon, only the daemon said, but the Java language mechanism is built on the basis of the JVM, meaning that the Java platform to the operating system to shield the bottom, So it can be in its own virtual platform to build a mechanism for their own advantage, while the language or platform designers are more or less to receive the influence of Unix, and the daemon mechanism is the JVM such a platform, so the Guardian thread emerged.

The role of Daemon is to provide services for the operation of other threads, such as GC threads. In fact, the user thread thread and the daemon thread daemon are essentially the same, 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 is out.

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

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

2), the new thread produced in the daemon thread is also daemon. (This is another essential distinction: the Daemon fork () is no longer a daemon, although it replicates the process-related information of the parent process, but the parent process of the subprocess process is not the INIT process, and the so-called daemon is essentially "the parent process hangs, the Init adopts, 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 since daemon thread has not yet come in and is operating.

Example:

The daemon task that completes the file output

Import java.io.*;

Class Testrunnable implements runnable{

public void Run () {

try{

Thread.Sleep (1000);//daemon blocking 1 seconds to run

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 up a daemon thread

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

}

}

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

But if you put Thread.setdaemon (true); Sets the daemon thread annotation, the file Daemon.txt can be written to the daemon string

The standard for the JRE to determine whether a program is finished is to have all front line Cheng completed, regardless of the background thread's state, so be sure to pay attention to this problem when using background threads.

But the actual application of daemon thread is there. For example, the servlet in the Web server, when the container is started, initializes a service thread, the dispatch thread, which handles the HTTP request, and then each request comes up to dispatch the thread to take a worker thread out of the thread pool to process the request to achieve concurrent control.

Online pick of a map to facilitate the understanding of everyone:


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.