On guard thread and non guard thread of Java

Recently, I re studied the basic knowledge of Java and found that too much knowledge has been omitted in the past. Compared with the threading mechanism of Java, there are two types of threads in Java: user thread and daemon thread (PS: previously ignored).

It is estimated that students who have studied unix development but have not carefully studied Java will be confused. There is no so-called concept of daemon thread in the operating system, only the daemon. However, the Java language mechanism is built on the basis of JVM, which means that the Java platform shields the bottom of the operating system, Therefore, it can construct a mechanism beneficial to itself in its own virtual platform, and the designer of the language or platform is more or less influenced by UNIX thought, and the daemon thread mechanism is suitable for platforms such as JVM, so the daemon thread came into being.

Daemon is used to provide services for the operation of other threads, such as GC threads. In fact, there is no difference between user thread and daemon thread in essence. The only difference is the departure of the virtual machine: if all user threads are evacuated, there will be no threads in daemon thread to serve, so the virtual machine will exit.

The daemon thread is not provided inside the virtual machine. Users can also set the daemon thread by themselves. Methods: public final void setdaemon (Boolean on); However, there are several points to note:

1)、thread. Setdaemon (true) must be in thread Set before start(), otherwise an illegalthreadstateexception will run. You cannot set a running regular thread as a daemon thread. (Note: This is obviously different from the daemon. After the daemon is created, let the 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; therefore, the language mechanism entrusted to the virtual machine is essentially different from the system level language.)

2) The new thread generated in the daemon thread is also daemon's. (this is another essential difference: the child process from the daemon fork () is no longer a daemon. Although it copies the process related information of the parent process, the parent process of the child process is not an init process, The so-called daemon essentially means "the parent process hangs, init adopts, and then the files 0, 1 and 2 are / dev / null, and the current directory is /")

3) . not all applications can be allocated to daemon threads for services, such as read-write operations or computing logic. Because the virtual machine may have exited before the daemon thread comes and operates.

example:

Run result: File daemon There is no "daemon" string in txt.

But if thread setDaemon(true); // Set the daemon thread and comment out the file daemon Txt can be written to the daemon string

The standard for JRE to judge whether the execution of the program ends is that all foreground execution threads have completed, regardless of the status of background threads. Therefore, you must pay attention to this problem when using background threads.

But where is the actual application of daemon thread? For example, the servlet in the web server initializes a service thread in the background when the container starts, that is, the scheduling thread, which is responsible for processing HTTP requests, and then each request scheduling thread takes a worker thread from the thread pool to process the request, so as to achieve the purpose of concurrency control.

A picture picked online is convenient for everyone to understand:

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
分享
二维码
< <上一篇
下一篇>>