Use daemon with caution

Source: Internet
Author: User
Public class test {public static void main (string [] ARGs) {int I = 1; system. Out. println ("main thread:" + I );}}

When we run the simple code above, many people think that the virtual machine runs only one main thread. In fact, virtual machines will run some service threads, such as garbage collection threads. This type of thread isDaemon thread
.

There are two types of threads in Java:User thread and daemon thread)

The role of daemon is to provide convenience services for the running of other threads. For example, the garbage collection thread is a very competent guardian. There is almost no difference between user and daemon. The only difference lies in the departure of virtual machines. If all user threads have exited and only the daemon thread exists, the virtual machine will quit.
Because daemon has no protection, there is no work to do, and there is no need to continue running the program.

It is worth mentioning that the daemon thread is not provided only within the virtual machine. You can also set the daemon thread when writing a program. The following method is used to set the daemon thread.

Public final void setdaemon (Boolean on)

Note the following points:

(1) thread. setdaemon (true) must be set before thread. Start (); otherwise, an illegalthreadstateexception occurs. You cannot set a running regular thread as a daemon thread.

(2) The new thread generated in the daemon thread is also daemon.
(3)
Do not consider that all applications can be assigned to daemon for service, such as read/write operations or computing logic.


Because it is impossible for you to know whether daemon has completed the expected service task before all the users are completed. Once the user exits, a large amount of data may not be read or written, and the computing task may run differently for multiple times. This is devastating to the program. The reason for this result has already been said: once all user threads leave, the virtual machine will quit.

// Complete the daemon task of file output import Java. io. *; Class testrunnable implements runnable {public void run () {try {thread. sleep (1000); // After the daemon.txt thread is blocked for 1 second, 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 (St Ring [] ARGs) throws interruptedexception {runnable TR = new testrunnable (); thread = new thread (TR); thread. setdaemon (true); // sets the daemon thread. start (); // start executing the sub-process} // running result: the daemon.txt file does not contain the "daemon" string.

As you can see, it is terrible to package the input and output logic into the daemon thread. the string is not written to the specified file. The reason is also very simple. The daemon thread is still blocked for 1 second until the main thread is completed. At this time, the main thread will soon run out, the virtual machine will quit, and daemon will stop the service, and the output operation will naturally fail.

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.