线程无法手动停止
public class TestStop implements Runnable{
public boolean flag=true;
@Override
public void run() {
int i=0;
while (flag){
System.out.println("run......."+i++);
}
}
public void stop(){
this.flag=false;
}
public static void main(String[] args) {
TestStop ts=new TestStop();
ts.run();
for (int i = 0; i < 1000; i++) {
System.out.println("main"+i++);
if (i==90){
ts.stop();
}
}
}
}
程序会进入死循环无法停止
求解答,可能是变量flag的问题,导致一直死循环
运行截图