package edu.wtbu;标签:void,System,demo01,停止,线程,public,out From: https://www.cnblogs.com/123456dh/p/17229395.html
public class Demo01 implements Runnable{
//1.设置一个标识位
private boolean flag=true;
@Override
public void run() {
int i=0;
while (flag){
System.out.println("run......."+i++);
}
}
//2.设置一个公开的方法停止线程
public void stop(){
this.flag=false;
}
public static void main(String[] args) {
Demo01 demo01 = new Demo01();
new Thread(demo01).start();
for (int i = 0; i <1000 ; i++) {
System.out.println("main"+i);
if(i==900){
//3用stop方法切换标志位,让线程停止
demo01.stop();
System.out.println("该线程停止了");
}
}
}
}