java System.exit(0) 结束不了其他线程

风格色 2016-08-25 03:05:26

书上说:如果某个线程调用了System.exit()指令来结束程序的运行,所有的线程都将结束。
然后我试验了一下,发现并不是这样的。
代码如下
public class Main {
public static void main(String[] args) {
Thread tA = new Thread(() -> {
for(int i = 0; i < 10; i++){
System.out.println("线程A "+i);
}
});
Thread tB = new Thread(() -> {
for(int i = 0; i < 10; i++){
System.out.println("线程B "+i);
if(i == 6){
System.out.println("程序强制退出");
System.exit(0);
}
}
});
tA.start();
tB.start();
}

控制台有时会这样打印:
线程B 0
线程B 1
线程B 2
线程B 3
线程B 4
线程B 5
线程B 6
线程A 0
程序强制退出
线程A 1
线程A 2
线程A 3
线程A 4
线程A 5
线程A 6
线程A 7
线程A 8
线程A 9

...全文
1289 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
我在第五层 2021-07-11
  • 打赏
  • 举报
回复

我觉得这个应该是结束当前线程和他的子线程

一颗开花的术 2017-10-30
  • 打赏
  • 举报
回复
将A线程变为while(true) 一直执行,就会发现A线程也会中止。两个线程各自执行,之前都循环十次,A线程可能在B线程调用System.exit(0)之前就执行完了
  • 打赏
  • 举报
回复
记得这个是结束主线程吧其他线程会在执行完后结束
  • 打赏
  • 举报
回复
public class Main {
    public static void main(String[] args) {
        Thread tA = new Thread(() -> {
            for(int i = 0; i < 10; i++){
                System.out.println("线程A "+i);
            }
        });
        Thread tB = new Thread(() -> {
            for(int i = 0; i < 10; i++){
                System.out.println("线程B "+i);
                if(i == 6){
                    System.out.println("程序强制退出");
                    System.exit(0);
                }
            }
        });
        tA.start();
        tB.start();
    }
   try {
	Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
	        System.out.println("hello");
这个是 System.exit();方法的注释Terminates the currently running Java Virtual Machine,确实是终止java虚拟机
  • 打赏
  • 举报
回复
没问题啊 没在线程里面结束的只是线程,又不是在main进程里面调用的
bree06 2016-09-05
  • 打赏
  • 举报
回复
看清楚题干, 如果某个线程调用了System.exit()指令来结束程序的运行,所有的线程都将结束。(你能在次线程结束掉主线程的运行??) 书上的意思是在主线程调用System.exit()会结束掉所有的线程. 在你例子中把ThreadB中的System.exit()放在start()的下面再运行.
tA.start();
tB.start();
System.exit(0);
bree06 2016-09-05
  • 打赏
  • 举报
回复
引用 7 楼 abcdefghiijklmnopqrs 的回复:
public class Main {
    public static void main(String[] args) {
        Thread tA = new Thread(() -> {
            for(int i = 0; i < 10; i++){
                System.out.println("线程A "+i);
            }
        });
        Thread tB = new Thread(() -> {
            for(int i = 0; i < 10; i++){
                System.out.println("线程B "+i);
                if(i == 6){
                    System.out.println("程序强制退出");
                    System.exit(0);
                }
            }
        });
        tA.start();
        tB.start();
    }
   try {
	Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
	        System.out.println("hello");
这个是 System.exit();方法的注释Terminates the currently running Java Virtual Machine,确实是终止java虚拟机
正确.将ThreadA中的for改为for (int i = 0; i < 100; i++)就能看出效果了. 之前我也理解错了
HerveyHall 2016-08-26
  • 打赏
  • 举报
回复
很好的问题,学习了
tatakautsubasa 2016-08-26
  • 打赏
  • 举报
回复
看来是书上写错了。书上出错挺正常的,所以很多书都会给个链接,指向勘误表。没勘误表的书建议别看。翻译的书建议配合原文对照看。

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧