package study_02;
/*终止线程
*1 线程正常执行完毕-->次数
*2 外部干涉--->加入标识
*不要使用 destroy和stop
*/
public class ThreadStop implements Runnable {
private boolean flag = true;
private String name;
public ThreadStop(String name) {
this.name = name;
}
@Override
public void run() {
int time = 0;
while (flag) {
System.out.println(name + "---->" + time++);
}
}
public void threadstop() {//外部终止线程
this.flag = false;
}
public static void main(String[] args) {
ThreadStop t = new ThreadStop("dan");
new Thread(t).start();//线程开启
for (int i = 0; i < 100; i++) {
if(i == 80) {
t.threadstop();//线程终止
System.out.println("stop");
}
System.out.println("main---->" + i);
}
}
}