java 如何在java中暂停线程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2537694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:40:13  来源:igfitidea点击:

How to pause a thread in java?

javamultithreading

提问by mithun1538

Consider the following code:

考虑以下代码:

while(true) { someFunction(); Thread.sleep(1000); }

while(true) { someFunction(); 线程睡眠(1000);}

What I want is that, someFunction() be called once every 10 seconds. But this is not the case. It is being called every second. I tried Thread.wait(1000), but even that doesnt help. I removed of the while part, just kept the body, and at the end wrote :

我想要的是, someFunction() 每 10 秒调用一次。但这种情况并非如此。每秒都在调用它。我试过 Thread.wait(1000),但即便如此也无济于事。我删除了 while 部分,只保留了正文,最后写道:

Thread.start();

线程开始();

But it throwed an exception. Is there any other solution to this?

但它抛出了一个异常。有没有其他解决方案?

回答by Jon Skeet

It's being called every second because you're sleeping for 1000 milliseconds, aka 1 second.

它每秒被调用一次,因为您睡眠了 1000 毫秒,也就是 1 秒。

Change it to Thread.sleep(10000)and that'll be better for you.

把它Thread.sleep(10000)改成这样对你会更好。

Alternatively, use

或者,使用

Thread.sleep(TimeUnit.SECONDS.toMillis(10));

which means you don't have to do the arithmetic yourself. (Many APIs now take a quantity and a TimeUnit, but there doesn't appear to be anything like that for Thread.sleepunfortunately.)

这意味着您不必自己进行算术运算。(许多 API 现在采用数量和TimeUnit,但Thread.sleep不幸的是,似乎没有类似的东西。)

Note that this will make the thread unresponsive for 10 seconds, with no clean way of telling it to wake up (e.g. because you want to shut it down). I generally prefer to use wait()so that I can pulse the same monitor from a different thread to indicate "I want you to wake up now!" This is usually from within a while loop of the form

请注意,这将使线程无响应 10 秒钟,没有干净的方法告诉它唤醒(例如,因为您想关闭它)。我通常更喜欢使用wait()这样我可以从不同的线程脉冲同一个监视器来指示“我希望你现在醒来!” 这通常来自表单的 while 循环内

while (!shouldStop())

EDIT: tvanfosson's solution of using a Timeris also good - and another alternative is to use ScheduledExecutorServicewhich can be a bit more flexible (and easier to test).

编辑:tvanfosson 使用 a 的解决方案Timer也很好 - 另一种选择是使用ScheduledExecutorService它可以更灵活(并且更容易测试)。

回答by Thomas L?tzer

Thread.sleep()takes the number of miliseconds to sleep. Thus, calling Thread.sleep(1000)sleeps 1000 miliseconds, which is 1 second. Make that Thread.sleep(10000)and it will sleep 10 seconds.

Thread.sleep()需要多少毫秒才能进入睡眠状态。因此,调用Thread.sleep(1000)休眠 1000 毫秒,即 1 秒。这样做,Thread.sleep(10000)它会休眠 10 秒。

回答by tvanfosson

What you probably want to do is use a Timerand set up a scheduled invocation of the function rather than sleeping. If you need more exactness with regard to the interval, you can keep track of how long it has been since the last run and adjust the new timer to keep your interval from drifting. See an example of a simple timer in this tutorial.

您可能想要做的是使用 aTimer并设置函数的预定调用而不是休眠。如果您需要更准确的间隔,您可以跟踪自上次运行以来已经过去了多长时间,并调整新计时器以防止您的间隔漂移。请参阅本教程中的简单计时器示例

I won't go into detail about the issue with regard to the precision of the argument, since @Jon has already covered that -- you need milliseconds rather than seconds.

我不会详细讨论有关参数精度的问题,因为@Jon 已经涵盖了这一点——您需要毫秒而不是秒。

回答by Buhake Sindi

1 second is 1000 milliseconds, so if you want 10 seconds = 10 * 1000 = 10000 milliseconds

1 秒是 1000 毫秒,所以如果你想要 10 秒 = 10 * 1000 = 10000 毫秒

e.g.

例如

try { 
     long numMillisecondsToSleep = 5000; // 5 seconds 
     Thread.sleep(numMillisecondsToSleep); 
} catch (InterruptedException e) { } 

回答by Venkat

Consider 1000 milliseconds is 1 second. For that you should use Thread.sleep(10000) for acheiving pause your thread in 10 seconds. You can also use looping with how many seconds to wait your thread. Ex. suppose you want to pause your thread in half-an-hour( 30 minutes). Then use,

考虑 1000 毫秒是 1 秒。为此,您应该使用 Thread.sleep(10000) 来实现在 10 秒内暂停您的线程。您还可以使用循环等待线程的秒数。前任。假设您想在半小时(30 分钟)后暂停您的线程。然后使用,

for(i=0;i<1800;i++)
{
   Thread.sleep(1000);
}

It will pause your thread in 30 minutes.

它会在 30 分钟后暂停您的线程。

回答by Michael Borgwardt

What I want is that, someFunction() be called once every 10 seconds.

我想要的是, someFunction() 每 10 秒调用一次。

Then why do you call Thread.sleep()with 1000 as parameter? That's in milliseconds, so you're explicitly saying "wait 1 second".

那你为什么Thread.sleep()用 1000 作为参数调用呢?这是以毫秒为单位,所以你明确地说“等待 1 秒”。