java主线程结束和子线程结束之间的关系

本文围绕Java主线程和子线程的关系展开。经测试得出:Main线程是不可设为守护线程的非守护线程;Main线程结束,其他非守护线程可正常运行,因线程不互相依赖;若子线程均为守护线程,Main线程结束,子线程会立刻结束,这是JVM底层机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:https://blog.youkuaiyun.com/aitangyong/article/details/16858273

最近在和同事讨论java主线程和子线程之间的关系,自己也到网上搜索了下,发现各种答案都有,有些还是互相矛盾的。经过测试自己得出以下几个结论,跟大家分享下,如果有错误,欢迎大牛指正,帮助我这只小菜鸟。废话不多说,直接上结论:

(一)Main线程是个非守护线程,不能设置成守护线程。

              这是因为,main线程是由java虚拟机在启动的时候创建的。main方法开始执行的时候,主线程已经创建好并在运行了。对于运行中的线程,调用Thread.setDaemon()会抛出异常Exception in thread "main"     java.lang.IllegalThreadStateException。测试代码如下:

public class MainTest{
    public static void main(String[] args){
        System.out.println(" parent thread begin ");
        Thread.currentThread().setDaemon(true);
    }
}

(二)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的底层实现机制,并不是说主线程和子线程之间存在依赖关系。
我的博客参考了http://bbs.youkuaiyun.com/topics/360195074这篇文章中大家的一些交流,碰撞出真知,大家的智慧是无穷的。

 

 

### Java 主线程子线程中的异常处理行为 在Java编程环境中,当主线程(main thread)或任何子线程(child threads)抛出未捕获的异常时,默认情况下JVM会打印堆栈跟踪(stack trace),随后对于主线程可能会终止程序执行;而对于子线程,则通常仅该特定线程结束不会影响到整个应用程序继续运行[^1]。 为了更好地管理这些情况,在设计阶段就应该考虑如何优雅地处理可能出现的各种错误条件。一种常见做法是在启动新线程的地方设置`Thread.UncaughtExceptionHandler`来监听并响应那些未能被内部逻辑捕捉下来的异常事件: ```java public class ThreadExceptionExample { public static void main(String[] args) { // 创建一个新的线程实例 Thread t = new Thread(() -> { throw new RuntimeException("Uncaught exception in thread"); }); // 设置未捕获异常处理器 t.setUncaughtExceptionHandler((thread, throwable) -> System.out.println( "Caught Exception from " + thread.getName() + ": " + throwable.getMessage())); // 启动线程 t.start(); try { t.join(); // 等待t线程完成 } catch (InterruptedException e) { Thread.currentThread().interrupt(); // 恢复中断状态 } } } ``` 此外,如果希望自定义异常类以便更精确地区分不同类型的失败情形,并允许调用者针对具体问题采取适当措施的话,可以遵循建议创建专门用于描述特殊情况下的异常类型[^2]。 通过这种方式不仅可以提高代码可读性维护性,同时也使得调试过程变得更加简单明了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值