Java 主线程结束与子线程结束之间的陷阱

问:Main 线程是守护线程吗?运行中的线程还能设置成守护线程吗?

答:main 线程是由 java 虚拟机在启动的时候创建的非守护线程。main 方法开始执行的时候,主线程已经创建好并在运行了。

对于运行中的线程,调用 Thread.setDaemon() 会抛出异常 Exception in thread "xxxx" java.lang.IllegalThreadStateException

守护线程是指在程序运行的时候在后台提供一种通用服务的线程,比如垃圾回收线程就是一个很称职的守护者,并且这种线程并不属于程序中不可或缺的部分。因此,当所有的非守护线程结束时,程序也就终止了,同时会杀死进程中的所有守护线程。反过来说,只要任何非守护线程还在运行,程序就不会终止。

Java 的守护线程与非守护线程

问:Main 线程中创建其他耗时的线程后自己结束了,耗时线程会跟着结束吗?

答:主线程,只是个普通的非守护线程,用来启动应用程序,不能设置成守护线程;除此之外,它跟其他非守护线程没有什么不同。主线程执行结束,其他线程一样可以正常执行。案例如下:

public class ParentTest {
    public static void main(String[] args) {
        System.out.println("parent thread begin");

        ChildThread t1 = new ChildThread("thread1");
        ChildThread t2 = new ChildThread("thread2");
        t1.start();
        t2.start();

        System.out.println("parent thread over ");
    }
}

class ChildThread extends Thread {
    private String name = null;

    public ChildThread(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        System.out.println(this.name + "--child thead begin");

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            System.out.println(e);
        }

        System.out.println(this.name + "--child thead over");
    }
}

结果如下:

parent thread begin 
parent thread over 
thread2--child thead begin
thread1--child thead begin
thread2--child thead over
thread1--child thead over

这样其实是很合理的,按照操作系统的理论,进程是资源分配的基本单位,线程是 CPU 调度的基本单位。对于 CPU 来说,其实并不存在 java 的主线程和子线程之分,都只是个普通的线程。进程的资源是线程共享的,只要进程还在,线程就可以正常执行,换句话说线程是强依赖于进程的。也就是说,线程其实并不存在互相依赖的关系,一个线程的死亡从理论上来说,不会对其他线程有什么影响。

问:Main 线程中创建其他耗时的守护线程后自己结束了,耗时的守护线程会跟着结束吗?

答:java 虚拟机(相当于进程)退出的时机是:虚拟机中所有存活的线程都是守护线程。只要还有存活的非守护线程虚拟机就不会退出,而是等待非守护线程执行完毕;反之,如果虚拟机中的线程都是守护线程,那么不管这些线程的死活 java 虚拟机都会退出。测试代码如下:

public class ParentTest {
    public static void main(String[] args) {
        System.out.println("parent thread begin ");

        ChildThread t1 = new ChildThread("thread1");
        ChildThread t2 = new ChildThread("thread2");
        t1.setDaemon(true);
        t2.setDaemon(true);

        t1.start();
        t2.start();

        System.out.println("parent thread over ");
    }
}

class ChildThread extends Thread {
    private String name = null;

    public ChildThread(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        System.out.println(this.name + "--child thead begin");
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            System.out.println(e);
        }
        System.out.println(this.name + "--child thead over");
    }
}

运行结果如下:

parent thread begin
parent thread over
thread1--child thead begin
thread2--child thead begin

在这种情况下,的确主线程退出后子线程就立刻结束了,但是这是属于 JVM 的底层实现机制,并不是说主线程和子线程之间存在依赖关系。

本文参考自 Java 主线程结束与子线程结束之间的坑

猜你喜欢

转载自blog.csdn.net/weixin_33924770/article/details/87477135