java daemon thread and non-daemon thread


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

Recently, I have re-studied the basic knowledge of Java and found that too much knowledge has been slightly touched before. Compared with the thread mechanism of Java, there are two types of threads in Java: User Thread (user thread), Daemon Thread (daemon thread), (PS: previously ignored).

       It is estimated that students who have studied Unix development but have not studied Java in detail will be confused. There is no so-called daemon thread in the operating system, only the daemon process, but the Java language mechanism is built on the basis of the JVM, meaning It is the Java platform that shields the bottom layer of the operating system, so it can construct a mechanism that is beneficial to itself in its own virtual platform, and the designers of the language or platform are more or less influenced by the idea of ​​Unix. The daemon thread mechanism is suitable for platforms such as JVM, so the daemon thread came into being.

       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 thread are essentially the same. The only difference is the departure of the virtual machine: if the User Thread is all evacuated, then the Daemon Thread will have no thread to serve, so The virtual machine also exits.

       The daemon thread is not provided by the virtual machine, and the user can also set the daemon thread by himself, the method: public final void setDaemon(boolean on); but there are a few points to note:

1), thread.setDaemon(true) must be set before thread.start(), otherwise an IllegalThreadStateException will run out. You cannot set a running regular thread as a daemon thread. (Note: This is obviously different from the daemon process. After the daemon process 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; The language mechanism of the 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 process fork() is no longer a daemon process, although it copies the process-related information of the parent process, but the parent process of the child process process is not the init process. , the so-called daemon process is essentially "the parent process hangs up, init adopts, and then files 0, 1, and 2 are all /dev/null, and the current directory goes to /")

3) Not all applications can be assigned to Daemon threads for services, such as read and write operations or computing logic. Because the virtual machine may have exited before the Daemon Thread has come and operated.

example:

//Complete the daemon thread task of file output

import java.io. *;  

 

class TestRunnable implements Runnable{  

    public void run(){  

               try{  

                  Thread.sleep(1000);//The daemon thread runs after blocking for 1 second  

                  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); //设置守护线程  

        thread.start(); //开始执行分进程  

    }  

}  

运行结果:文件daemon.txt中没有"daemon"字符串。

但是如果把thread.setDaemon(true); //设置守护线程注释掉,文件daemon.txt是可以被写入daemon字符串的

 

  JRE判断程序是否执行结束的标准是所有的前台执线程行完毕了,而不管后台线程的状态,因此,在使用后台线程候一定要注意这个问题。 

但是daemon Thread实际应用在那里呢?举个例子,web服务器中的Servlet,容器启动时后台初始化一个服务线程,即调度线程,负责处理http请求,然后每个请求过来调度线程从线程池中取出一个工作者线程来处理该请求,从而实现并发控制的目的。

网上摘的一个图,方便大家理解:

 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325465836&siteId=291194637