Welcome to search “little monkey technical notes” to follow my public account, you can timely communicate with me if you have any questions.
Daemon threads are support threads that are used for background scheduling and support work. Daemons have automatic end-of-life features that non-daemons do not. If you don’t understand the above concepts, let’s take a look at a simple example of daemon thread execution:
Public class MyDaemon implements Runnable {@override public void run() {system.out.println ("******** daemon ********"); }}Copy the code
public class MyDaemonTest { public static void main(String[] args) { Thread thread = new Thread(new MyDaemon(), "Daemon thread "); thread.setDaemon(true); thread.start(); System.out.println("******main thread completes ******"); }}Copy the code
Please take a guess at the result of the above execution. If you execute it many times, you will find that the result of the execution will be two things. The first is most likely to occur, and the second occurs occasionally after running the above program many times.
* * * * * * the main thread end * * * * * * * * * * * * the main thread end * * * * * * * * * * * * * * * * * * * * * * daemonCopy the code
If you don’t understand the true meaning of daemons, why do these two situations occur? I’ll use this example to explain the description of daemon threads at the beginning of this article.
First, a thread must be started by another thread. In the above example, we ran the “main()” method, which started the main thread. The “main()” method then calls our custom thread “MyDaemon()”, which we then set up as a daemon. A characteristic of daemons is that when all non-daemons have finished running, the JVM exits and the process terminates. Let me draw a picture to show you what that means.To verify the above expression, let’s modify the previous “MyDaemon()” and compare the results:
public class MyDaemon implements Runnable { @Override public void run() { while (true) { System.out.println("******** daemon ********"); }}}Copy the code
Set the daemon in MyDaemonTest() to false.
public class MyDaemonTest { public static void main(String[] args) { Thread thread = new Thread(new MyDaemon(), "Daemon "); thread.setDaemon(false); thread.start(); System.out.println("******main thread completes ******"); }}Copy the code
The result of this thread running is that the contents of “MyDaemon()” are looping in a consistent fashion and the JVM does not exit.
Thread.setdaemon (true); thread.setdaemon (true); thread.setdaemon (true);
public class MyDaemonTest { public static void main(String[] args) { Thread thread = new Thread(new MyDaemon(), "Daemon "); thread.setDaemon(true); thread.start(); System.out.println("******main thread completes ******"); }}Copy the code
The result of this run is that the JVM can exit automatically.
Suppose you want a less important thread to execute while the main thread is running, but you want the less important thread to terminate as soon as the main thread exits. You might have thought of using interrupts, but it would be easier to set up less important threads for use by daemons.
Note: When building Daemon threads, you cannot rely on the contents of the finally block to ensure that the logic to close or clean up a resource is executed, as it may exit without being executed. Welcome to search “little monkey technical notes” to follow my public account, you can timely communicate with me if you have any questions.