62,620
社区成员
发帖
与我相关
我的任务
分享import java.util.Date;
public class SleepThreadTest {
public static void main(String[] args) {
run1 r = new run1();
Thread t = new Thread(r);
while (true) {
t.start();
try {
t.sleep(1000);
} catch (InterruptedException e) {
return;
}
}
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
t.interrupt();
}
}
class run1 implements Runnable {
public void run() {
System.out.println(new Date());
}
}
Thread t = new Thread(c)
t.start();
sleep(10000);//睡个10s
t.interrupt();
void run()
{
while(true){
System.out.ptintln(new Date());
sleep(100)//睡个0.1s
}
}
因为 while (true) 是死循环 后面 代码 执行不到的。