package com.anyan;
/**
* @author anyan
* @date 2021/5/4-16:07
*/
/*
改进后,终止线程的第二种方法:
*/
public class ThreadEndTest02 {
public static void main(String[] args) {
//Thread t=new Thread();
Student6 stu6=new Student6();
stu6.setName("分支线程");
stu6.start();
for(int j=0;j<30;j++){
for (int i = 0; i <50; i++) {
try {
System.out.println(Thread.currentThread().getName());
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
stu6.run=false;//使分支线程提前终止
}
}
}
class Student6 extends Thread {
boolean run=true;
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if(run){
// Thread.currentThread().setName("分支线程");
try {
System.out.println(Thread.currentThread().getName());
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
//在进程终止前save想要保存的内容
System.out.println("分支线程被终止");
return;
}
}
}
}