50,526
社区成员
发帖
与我相关
我的任务
分享
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);