java线程遇到的小问题
为什么下面的代码在main的方法中加上 System.out.println("main"+i); 子线程才会正常执行,
如果不加,子线程没有任何输出?
自己尝试让主线程 sleep 又可以正常输出,求大佬解惑
public class TestStop implements Runnable{
private boolean flag = true;
@Override
public void run() {
int i= 1000;
while (i > 0) {
System.out.println(flag);
System.out.println("run"+i--);
}
}
public void stop(){
this.flag = false;
}
public static void main(String[] args) throws InterruptedException {
TestStop testStop = new TestStop();
new Thread(testStop).start();
for (int i = 0; i < 100000; i++) {
// System.out.println("main"+i);
if (i==900) {
//切换标志位让线程停止
testStop.stop();
System.out.println("线程停止");
}
}
}
}#java多线程##申通快递#
查看9道真题和解析
