Java:为什么在Thread的run()方法中不能使用sleep()暂停线程呢?
publicclassBarWorkerimplementsRunnable{privatestaticbooleanflag=false;publicvoidrun()...
public class BarWorker implements Runnable {
private static boolean flag = false;
public void run() {
if (!flag) {
sleep(2000); // 此处编译不通过
flag = true;
System.out.println("Enter");
System.out.println("Work");
System.out.println("Leave");
flag = false;
} else {
System.out.println("Stop");
}
}
} 展开
private static boolean flag = false;
public void run() {
if (!flag) {
sleep(2000); // 此处编译不通过
flag = true;
System.out.println("Enter");
System.out.println("Work");
System.out.println("Leave");
flag = false;
} else {
System.out.println("Stop");
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询