Java daemon and non-daemon thread __java

Source: Internet
Author: User

Http://www.cnblogs.com/super-d2/p/3348183.html has two types of threads in Java: user thread (user threads) , Daemon The thread (daemon)
Daemon is useful 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 on its own, method: public final void Setdaemon (Boolean on); but there are a few caveats: Thread.setdaemon (True) Must be set before Thread.Start (), otherwise a illegalthreadstateexception exception will run out. You cannot set a running general 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. The new thread that is generated in the daemon thread is also the 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/"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. 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.

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.