Daemon thread in Java
Daemon thread in Java
There are two types of threads in Java, user threads and daemon threads.
User threads is a high priority thread. The JVM will wait for all user threads to run before it ends.
Daemon threads are low priority threads that serve user threads. Because daemon threads have a low priority and only serve user threads, the JVM will automatically exit when all user threads are finished, regardless of whether there are still daemon threads running.
Because of this feature, we usually handle infinite loop operations in daemon threads, because this will not affect the operation of user threads.
Daemon threads is not recommended for I / O operations.
However, some improper operations may also cause daemon threads to block JVM closes, such as calling join () in daemon thread.
Let's see how to create a daemon thread:
public class DaemonThread extends Thread{
public void run(){
while(true){
log.info("Thread A run");
try {
log.info("Thread A is daemon {}" ,Thread.currentThread().isDaemon());
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
DaemonThread daemonThread = new DaemonThread();
daemonThread.setDaemon(true);
daemonThread.start();
}
}
Creating a daemon thread is very simple. You only need to set its daemon property to true after creation.
The above example will exit immediately.
If we will daemonthread setDaemon(true); If it is removed, the JVM will continue to run and will not exit because the user thread has been executed.
This is the case when running in main. What happens if we run in a @ test?
public class DaemonThread extends Thread{
public void run(){
while(true){
log.info("Thread A run");
try {
log.info("Thread A is daemon {}" ,Thread.currentThread().isDaemon());
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Test
public void testDaemon() throws InterruptedException {
DaemonThread daemonThread = new DaemonThread();
daemonThread.start();
}
}
We changed the main method to @ test execution. After execution, we will find that whether it is a daemon thread or not, the test will end immediately.
Let's look at the case where a user thread is started in a daemon thread:
public class DaemonBThread extends Thread{
Thread worker = new Thread(()->{
while(true){
log.info("Thread B run");
log.info("Thread B is daemon {}",Thread.currentThread().isDaemon());
}
});
public void run(){
log.info("Thread A run");
worker.start();
}
public static void main(String[] args) {
DaemonBThread daemonThread = new DaemonBThread();
daemonThread.setDaemon(true);
daemonThread.start();
}
}
In this example, the daemon thread starts a user thread. After running, we will find that even if a user thread is running, the JVM will immediately end execution.
Examples of this article can be referred to https://github.com/ddean2009/learn-java-concurrency/tree/master/DaemonThread
For more tutorials, please refer to flybean's blog