线程5怎么无法启动?

qq_15357533 qq_15357533 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

public class Hello18 {
public static LinkedBlockingQueue<String> mLQ = new LinkedBlockingQueue<>();

public static void main(String[] args) {
Hello18.mLQ.offer("中国");
Thread thread1 = new Thread(new ARunnable(), "线程1");
thread1.start();

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}

Hello18.mLQ.poll(); //去掉这行或者把 refreshTime >= 3000L
//改为 30000L,线程1接着打印时,线程5也能启动

try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

Hello18.mLQ.offer("韩国");

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}

new Thread(new ARunnable(), "线程5").start();
}
}


class ARunnable implements Runnable {
public static volatile boolean can = true;

@Override
public void run() {
int i = 0;
long refreshTime = System.currentTimeMillis();
while (can) {
if (!Hello18.mLQ.isEmpty()) {
refreshTime = System.currentTimeMillis();
System.out.println(Thread.currentThread().getName() + " i = " + i);
i++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
if (System.currentTimeMillis() - refreshTime >= 3000L) {
can = false; //终止线程
}
}
}
}
}
...全文
评论 3
收藏 已收藏
分享
返回顶部
qq_15357533
Java SE 62,621 成员 307,251 内容