我有一个像这样的for循环:
for (int i = 0; i < 10; i++)
{
//Do something
//Delay for 5 seconds then go to the next iteration of loop
}
如何在Java中指定延迟?
答案 0 :(得分:1)
执行此操作的默认方法是Thread.sleep(5000)。通常它在multithreading的上下文中使用。
for (int i = 0; i < 10; i++)
{
//Do something
//Delay for 5 seconds then go to the next iteration of loop
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}