老师,关于sleep我有很大的疑惑
来源:5-3 如何停止线程

大锤电气焊
2019-12-20
Thread.sleep()应该是睡眠了main线程吧,然后main线程睡眠期间,新建的子线程是还在运行还是也是睡眠状态?
如果我代码如下,为何新建线程就不运行了?
public class RightWayStopThreadWithoutSleep implements Runnable{
@Override
public void run() {
int num=0;
int count=0;
while(!Thread.currentThread().isInterrupted() && num<Integer.MAX_VALUE/2){
if(num%10000==0){
System.out.println(num+"是10000的倍数");
count++;
}
num++;
}
System.out.println("任务运行结束了,运行次数为"+count);
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new RightWayStopThreadWithoutSleep());
thread.start();
// Thread.sleep(1000);
thread.interrupt();
}
}
写回答
2回答
-
悟空
2019-12-20
睡眠的是主线程 子线程不影响
00 -
大锤电气焊
提问者
2019-12-20
还是说其实睡眠的是当前正在执行的线程也就是新建的子线程?
00
相似问题