In Java, will the program stop when the main thread ends?

In Java, when maina method finishes executing, it doesn't mean the program stops immediately. Instead, program execution continues until all non-daemon threads have terminated.

In Java, there are two types of threads: user threads and daemon threads. mainThe thread started by the method is a user thread by default. When all user threads have finished executing, the Java Virtual Machine exits automatically, causing the program to stop.

A daemon thread is a thread that provides services for other threads. When all user threads are finished, the daemon thread will automatically exit. For example, the garbage collector thread is a daemon thread that runs in the background and is responsible for reclaiming memory that is no longer in use.

If you want to stop the program immediately after mainthe method ends, you can set all non-daemon threads as daemon threads. A thread can be set as a daemon thread using setDaemon(true)the method . When all non-daemon threads have finished executing, the program stops immediately.

Here is a sample code that demonstrates how to set up a daemon thread:

public class DaemonThreadExample {
    
    
    public static void main(String[] args) {
    
    
        Thread daemonThread = new Thread(new DaemonTask());
        daemonThread.setDaemon(true); // 将线程设置为守护线程
        daemonThread.start();

        // 主线程执行一段时间
        try {
    
    
            Thread.sleep(5000);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }

        System.out.println("Main thread finished.");
    }

    static class DaemonTask implements Runnable {
    
    
        @Override
        public void run() {
    
    
            while (true) {
    
    
                System.out.println("Daemon thread is running.");
                try {
    
    
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }
}

In the above example, a daemon thread is created in the methodDaemonThreadExample of the class and set as a daemon thread. Then after the main thread executes for a period of time, a message is printed to indicate that the method execution is complete. At this point, only the daemon thread is left running, outputting the "Daemon thread is running." message. The program then stops immediately and terminates the execution of the daemon thread.maindaemonThreadmain

To sum up, when the main thread ends, the program will stop immediately and will not wait for the daemon thread to finish executing.

It should be noted that when the program terminates, all executing threads will be interrupted and will not be completely executed. Therefore, when designing a multithreaded program, it is necessary to ensure that all threads are properly terminated to avoid resource leaks and other potential problems.

Guess you like

Origin blog.csdn.net/a772304419/article/details/131024132