Detailed explanation of Java thread daemon thread and user thread
- 2021-07-16 02:19:48
- OfStack
Dry java development for so many years, before 1 straight did not pay attention to java process also distinguish between daemon process and user process. The concept of daemon was first contacted in linux system. Until recently, when java was used to develop heartbeat detection function, it was discovered that java also had the concept of daemon thread when Timer was used.
1. Java Thread
1.1 Daemon and User Threads
Java threads are divided into daemon threads (DaemonThread) and user threads (UserThread).
Normally, the threads we create with Thread are user threads by default, and when thread. setDaemon (true) is executed before starting the thread, the thread becomes a daemon thread. In fact, in essence, user threads and daemon threads are not much different, the only difference is that it will affect the exit of virtual machines (termination of programs). When only the daemon thread is left in jvm, the virtual opportunity exits and the program terminates; When there is at least one user thread in jvm, jvm will not exit.1.2 Guardian Thread Features
The daemon thread in Java is different from the daemon process in linux. The daemon process in linux is at the system level and terminates when the system exits. While the daemon thread in java is at the jvm level, when there is no user process in jvm, the daemon process is destroyed, jvm exits and the program terminates. The author thinks that the most important characteristics of java daemon process are:
A daemon is a thread running in the background of a program The process created by the daemon is still the daemon The daemon does not affect the exit of jvm, and jvm exits when only daemons remain in jvm The guardian is automatically destroyed when jvm exitsWhen developing java daemon threads, you should pay attention to:
Set thread. setDemon (true) before the thread starts and before the thread. start () method is executed; Child threads started in a daemon are also daemons The daemon does not recommend writing because the daemon may end at any time.1.3 Scenarios applicable to daemon threads
According to the characteristics of daemon thread, the author thinks that java daemon thread can usually be used to develop some functions for other user threads. For example, heartbeat detection, event monitoring and so on. The most famous daemon in Java is GC (garbage collection)
2. java daemon thread development
There are two things to pay attention to when developing daemons:
The thread. setDaemon (true) method must be executed before the thread starts, that is, before the thread. start () method is executed, or the exception IllegalThreadStateException is thrown Threads created in daemon threads are also daemon threads2.1 Test program exit
Note thread. setDaemon (true), the thread for the user thread, the program wireless loop, the program does not terminate. After setting thread. setDaemon (true), the thread becomes a daemon thread, and the program terminates directly, only outputting one line of information "main thread done". jvm exits because after the program executes the system statement, the main program executes as only one user thread, and there is only one daemon left in jvm.
public class TestThread {
public static void main(String[] args) {
AnsyTask ansyTask = new AnsyTask();
Thread thread = new Thread(ansyTask);
// Set threads to asynchronous threads
// thread.setDaemon(true);
// Startup thread
thread.start();
System.out.println("main thread done");
}
}
class AnsyTask implements Runnable{
@Override
public void run() {
while (true){
System.out.println(LocalDateTime.now() + "-hello,thread");
}
}
}
2.2 Create a new thread in the test daemon thread
Tests show that, by default, child threads created by daemons are still daemons, and daemons created by users are still user threads. It can also be modified by setDaemon () method when creating child threads.
public class TestThread {
public static void main(String[] args) throws InterruptedException {
AnsyTask ansyTask = new AnsyTask();
Thread thread = new Thread(ansyTask);
// Set threads to asynchronous threads
thread.setDaemon(true);
// Startup thread
thread.start();
// Give the daemon a little execution time
Thread.sleep(1000l);
}
}
class AnsyTask implements Runnable{
@Override
public void run() {
Thread thread = new Thread("subThread");
System.out.println(thread.getName() + " is daemon:" + thread.isDaemon());
}
}