Java daemon thread

When Java Concurrency in Practice or see Java-related code involves concurrent, often encounter some threads (such as doing statistical metrics thread, etc.) will by  setDaemon() setting the thread's daemon variable is set to True method, that is, this thread In order to set the daemon threads (daemon the thread) , then what is the daemon thread it? Or the difference between non-daemon threads and daemon threads (common thread) in place? This is mainly about the content of this article.

Daemon thread

In general, the Java thread can be divided into two types: ordinary thread and thread guard. When JVM just started, all the threads it creates, in addition to the main thread (main thread), the other thread is a daemon thread (for example: garbage collector, and other secondary action thread).

When you create a new thread, a new thread will inherit the daemon status of its thread, by default, all the threads created by the main thread are common threads.

Under what circumstances would require daemon threads it? Under normal circumstances, when we want to create a secondary thread to perform some work, but do not want to close this thread hinder the JVM, in this case, we need to use a daemon thread.

The role of guardian of the thread

Daemon thread with the common thread is the only difference: when the thread exits, JVM will check other threads running, if these threads are daemon threads, the JVM exits normally operate, but if there are common threads still running, JVM is exit does not perform the operation. When JVM exits, all remains the guardian of the thread will be abandoned, neither part of the implementation of the code finally, it will not perform stack unwound operation, JVM will exit.

When the JVM halts any remaining daemon threads are abandoned:

1. finally blocks are not executed,
2. stacks are not unwound - the JVM just exits.

Here there is a small example, from  the What IS A daemon the Thread in the Java? , Code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class DaemonTest {

public static void main(String[] args) {
new WorkerThread().start();

try {
Thread.sleep(7500);
} catch (InterruptedException e) {
// handle here exception
}

System.out.println("Main Thread ending");
}

}

class WorkerThread extends Thread {

public WorkerThread() {
// When false, (i.e. when it's a user thread), the Worker thread continues to run.
// When true, (i.e. when it's a daemon thread), the Worker thread terminates when the main thread terminates.
setDaemon(false);
}

public void run() {
int count = 0;

while (true) {
System.out.println("Hello from Worker " + count++);

try {
sleep(5000);
} catch (InterruptedException e) {
// handle exception here
}
}
}
}

When the thread is normal, the output is as follows:

Hello from Worker 0
Hello from Worker 1
Main Thread ending
Hello from Worker 2
Hello from Worker 3
Hello from Worker 4
Hello from Worker 5
....

In other words, this time even if the main thread of execution is over, JVM will wait WorkerThread finished will quit, and if the thread is a daemon thread set, then output as follows:

Hello from Worker 0
Hello from Worker 1
Main Thread ending

在 main 线程执行完毕后,JVM 进程就退出了,不会 care WorkerThread 线程是否执行完毕。

参考:

文章来源:https://matt33.com/2018/07/07/java-daemon-thread/

Guess you like

Origin www.cnblogs.com/fanfuhu/p/12315468.html