Java 延迟/等待

发布于 2024-12-22 12:06:20 字数 63 浏览 7 评论 0原文

如何将 while 循环延迟到 1 秒间隔,而不减慢其运行的整个代码/计算机的速度到一秒延迟(只是一个小循环)。

How do I delay a while loop to 1 second intervals without slowing down the entire code / computer it's running on to the one second delay (just the one little loop).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

相思故 2024-12-29 12:06:20
Thread.sleep(1000); // do nothing for 1000 miliseconds (1 second)
Thread.sleep(1000); // do nothing for 1000 miliseconds (1 second)
梦幻的味道 2024-12-29 12:06:20

看起来你的循环在主线程上运行,如果你在该线程上睡眠,它将暂停应用程序(因为只有一个线程已暂停),为了克服这个问题,你可以将此代码放入并行运行的新Thread

try{

  Thread.sleep(1000);
}catch(InterruptedException ex){
  //do stuff
}

It seems your loop runs on Main thread and if you do sleep on that thread it will pause the app (since there is only one thread which has been paused), to overcome this you can put this code in new Thread that runs parallely

try{

  Thread.sleep(1000);
}catch(InterruptedException ex){
  //do stuff
}
大姐,你呐 2024-12-29 12:06:20

我延迟循环的简单方法。

在未能遵循 stackoverflow 的标准之后,我已经将代码放在这里了。

//1st way: Thread.sleep : Less efficient compared to 2nd
try {
  while (true) {//Or any Loops
   //Do Something
   Thread.sleep(sleeptime);//Sample: Thread.sleep(1000); 1 second sleep
  }
 } catch (InterruptedException ex) {
   //SomeFishCatching
 }
//================================== Thread.sleep


//2nd way: Object lock waiting = Most efficient due to Object level Sync.
Object obj = new Object();
 try {
  synchronized (obj) {
   while (true) {//Or any Loops
   //Do Something
   obj.wait(sleeptime);//Sample obj.wait(1000); 1 second sleep
   }
  }
 } catch (InterruptedException ex) {
   //SomeFishCatching
 }
//=============================== Object lock waiting

//3rd way:  Loop waiting = less efficient but most accurate than the two.
long expectedtime = System.currentTimeMillis();
while (true) {//Or any Loops
   while(System.currentTimeMillis() < expectedtime){
     //Empty Loop   
   }
   expectedtime += sleeptime;//Sample expectedtime += 1000; 1 second sleep
   //Do Something
}
//===================================== Loop waiting

My simple ways to delay a loop.

I already put the codes here after failing to follow the stackoverflow's standards.

//1st way: Thread.sleep : Less efficient compared to 2nd
try {
  while (true) {//Or any Loops
   //Do Something
   Thread.sleep(sleeptime);//Sample: Thread.sleep(1000); 1 second sleep
  }
 } catch (InterruptedException ex) {
   //SomeFishCatching
 }
//================================== Thread.sleep


//2nd way: Object lock waiting = Most efficient due to Object level Sync.
Object obj = new Object();
 try {
  synchronized (obj) {
   while (true) {//Or any Loops
   //Do Something
   obj.wait(sleeptime);//Sample obj.wait(1000); 1 second sleep
   }
  }
 } catch (InterruptedException ex) {
   //SomeFishCatching
 }
//=============================== Object lock waiting

//3rd way:  Loop waiting = less efficient but most accurate than the two.
long expectedtime = System.currentTimeMillis();
while (true) {//Or any Loops
   while(System.currentTimeMillis() < expectedtime){
     //Empty Loop   
   }
   expectedtime += sleeptime;//Sample expectedtime += 1000; 1 second sleep
   //Do Something
}
//===================================== Loop waiting
空心↖ 2024-12-29 12:06:20

正如 Jigar 所指出的,您可以使用另一个线程来完成可以独立于其他线程运行、睡眠等的工作。 java.util.Timer 类也可能对您有所帮助,因为它可以为您执行定期任务,而无需您进行多线程编程。

As Jigar has indicated you can use another Thread to do work which can operate, sleep etc independently of other Threads. The java.util.Timer class might help you as well since it can perform periodic tasks for you without you having to get into multithreaded programming.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文