On guard thread and user thread in Java

Java threads are divided into two types: daemon threads and user threads (user thread), the main function will be called when the JVM is started. The thread where the main function is located is a user thread, which we can see. In fact, many daemon threads, such as garbage collection threads, are also started inside the JVM. What is the difference between daemon threads and user threads? One of the differences is that when the last non daemon thread ends, the JVM It will exit normally, regardless of whether there is a daemon thread at present, that is, whether the daemon thread ends or not does not affect the exit of the JVM. The implication is that as long as one user thread has not ended, the JVM will not exit under normal circumstances.

So how to create a daemon thread in Java? The code is as follows:

You only need to set the daemon parameter of the thread to true.

The following is an example to deepen the understanding of the difference between user threads and daemon threads. First, look at the following code:

The result output is:

The above code creates a thread thread in the main thread, which is an infinite loop. From the result of running the code, the main thread has finished running, so the JVM has exited? The Red Square on the right side of the IDE output indicates that the JVM process has not exited. In addition, executing PS - EAF | grep Java on the Mac will output the result, which can also prove this conclusion.

This result shows that when the parent thread ends, the child thread can still exist, that is, the life cycle of the child thread is not affected by the parent thread. It also shows that the JVM process will not terminate when the user thread still exists. Then, let's set the above thread as a daemon thread and see what effect it will have:

The execution results are:

As mentioned above, set the thread as the daemon thread before starting the thread. From the output results, it can be seen that the JVM process has been terminated and the JVM process can not be seen when executing PS - EAF | grep Java. In this example, the main function is the only user thread, and the thread thread is the daemon thread. When the main thread runs, the JVM finds that there are no user threads, it will terminate the JVM process.

In Java, after the main thread runs, the JVM will automatically start a destroyjavavm thread, which will wait for all user threads to terminate the JVM process. The following is a simple JVM code to prove this conclusion:

Open the JVM code and eventually call the C function javamain

Leave is a macro definition in C language, which is defined as follows:

The above macro actually creates a thread called destroy JavaVM to wait for all user threads to end.

Summary: if you want the JVM process to end immediately after the main thread ends, you can set the thread as the daemon thread when creating the thread. Otherwise, if you want the sub thread to continue working after the main thread ends and let the JVM process end after the sub thread ends, you can set the sub thread as the user thread, The open source framework Tomcat uses guard threads and user threads to run together. For details, please look forward to the publication of the foundation of Java Concurrent Programming and source code analysis.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>