51,411
社区成员
发帖
与我相关
我的任务
分享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();
} 我觉得这个应该是结束当前线程和他的子线程
其他线程会在执行完后结束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虚拟机tA.start();
tB.start();
System.exit(0);
正确.将ThreadA中的for改为for (int i = 0; i < 100; i++)就能看出效果了. 之前我也理解错了
很好的问题,学习了