我正在尝试用Java做一些事情,我需要在while循环中等待/延迟一段时间。
while (true) {
if (i == 3) {
i = 0;
}
ceva[i].setSelected(true);
// I need to wait here
ceva[i].setSelected(false);
// I need to wait here
i++;
}
我想构建一个步序器,我是Java的新手。有什么建议吗?
答案 0 :(得分:533)
如果您想暂停,请使用java.util.concurrent.TimeUnit
:
TimeUnit.SECONDS.sleep(1);
睡一秒或
TimeUnit.MINUTES.sleep(1);
睡一会儿。
由于这是一个循环,这提出了一个固有的问题 - 漂移。每次你运行代码然后再睡觉时,你会每隔一秒就跑步一次。如果这是一个问题,请不要使用sleep
。
此外,sleep
在控制方面并不是非常灵活。
为了每秒或延迟一秒运行任务,我强烈推荐ScheduledExecutorService
以及scheduleAtFixedRate
或scheduleWithFixedDelay
。
例如,要每秒运行方法myTask
(Java 8):
public static void main(String[] args) {
final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleAtFixedRate(App::myTask, 0, 1, TimeUnit.SECONDS);
}
private static void myTask() {
System.out.println("Running");
}
在Java 7中:
public static void main(String[] args) {
final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
myTask();
}
}, 0, 1, TimeUnit.SECONDS);
}
private static void myTask() {
System.out.println("Running");
}
答案 1 :(得分:124)
使用Thread.sleep(1000)
;
1000
是程序暂停的毫秒数。
try
{
Thread.sleep(1000);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
答案 2 :(得分:6)
您需要使用Thread.sleep()
来电。
此处有更多信息:http://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html
答案 3 :(得分:3)
使用Thread.sleep(100); 时间单位是毫秒
例如
public class SleepMessages {
public static void main(String args[])
throws InterruptedException {
String importantInfo[] = {
"Mares eat oats",
"Does eat oats",
"Little lambs eat ivy",
"A kid will eat ivy too"
};
for (int i = 0;
i < importantInfo.length;
i++) {
//Pause for 4 seconds
Thread.sleep(4000);
//Print a message
System.out.println(importantInfo[i]);
}
}
}
答案 4 :(得分:2)
使用此:
public static void wait(int ms){
try
{
Thread.sleep(ms);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
然后您可以在任何地方调用此方法,例如:
wait(1000);
答案 5 :(得分:1)
我知道这是一篇很老的文章,但这可能对某人有帮助:
您可以创建一个方法,因此无论何时需要暂停,都可以键入pause(1000)
或任何其他毫秒值:
public static void pause(int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
System.err.format("IOException: %s%n", e);
}
}
它被插入到类内部public static void main(String[] args)
的正上方。然后,要调用该方法,请键入pause(ms)
,但将ms
替换为要暂停的毫秒数。这样,您就不必在要暂停时插入整个try-catch语句。
答案 6 :(得分:0)
使用TimeUnit.SECONDS.sleep(1);
或Thread.sleep(1000);
是可接受的方式。在这两种情况下,您都必须抓住InterruptedException
,这会使您的代码变得笨拙。有一个名为MgntUtils(由我编写)的开源Java库,提供了已经在内部处理InterruptedException
的实用程序。因此,您的代码将只包含一行:
TimeUtils.sleepFor(1, TimeUnit.SECONDS);
请参阅javadoc here。您可以从Maven Central或Github访问库。可以在here
中找到有关该库的文章。答案 7 :(得分:0)
`
while (true) {
if (i == 3) {
i = 0;
}
ceva[i].setSelected(true);
try{Thread.sleep(time_in_milliseconds);}catch(InterruptedException ie){}
ceva[i].setSelected(false);
try{Thread.sleep(1000);}catch(InterruptedException ie){}
i++;
}
1second = 1000毫秒 `
答案 8 :(得分:-3)
我正在使用的一种快速解决方案是(等待两秒钟):
long startTime = System.currentTimeMillis();
while(System.currentTimeMillis() - startTime < 2000) { }
在选择System.nanoTime()上的System.currentTimeMillis()时,我提到了这一点: https://www.geeksforgeeks.org/java-system-nanotime-vs-system-currenttimemillis/
免责声明:我对编码还比较陌生,这在某种程度上很不安全。我无法在网上找到任何东西反对使用这样的循环,但是我不想保证这是解决您问题的安全解决方案。