父线程停止是否会导致子线程停止

父线程停止是否会导致子线程停止呢?

不会,具体可看测试程序:

public class MultiThread {  
    public static void main(String args[]) throws Exception {  
        boolean flag = true;  
        while (flag) {  
            Thread fatherThread = new FatherThread();  
            fatherThread.start();  
            System.out.println("========");  
            flag = false;  
        }  
    }// main  
}// MultiThread  
  
class FatherThread extends Thread{  
    FatherThread() {  
    }// 构造函数  
  
    public void run() {  
    	System.out.println("father thread start ...");  
    	childThread childThread = new childThread(this);  
        childThread.start();  
        System.out.println("father thread sleep 100000"); 
        try {  
        	//把这个值设置的足够大就看到在子线程停止以后是否还有线程存在
        	//不存在说明子线程在运行5秒以后是有将父线程停止的
            Thread.sleep(100000);  
        } catch (InterruptedException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }  
}  
  
class childThread extends Thread {  
    private FatherThread thread;  
    childThread(FatherThread thread) {  
        this.thread = thread;  
    }// 构造函数  
  
    public void run() {  
    	System.out.println("child thread start ...");
        try {  
            Thread.sleep(5000);  
        } catch (InterruptedException e1) {  
            // TODO Auto-generated catch block  
            e1.printStackTrace();  
        }  
        System.out.println(thread.getState());
        thread.stop();  
        System.out.println("father thread stop ...");  
        System.out.println("child thread continue ...");  
        System.out.println("child thread sleep 5000");  
        try {  
            Thread.sleep(5000);  
        } catch (InterruptedException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        //将状态效果打印到这就能很明显的看到效果
        System.out.println(thread.getState());
        System.out.println(thread.isAlive());
    }  
}  
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值