简体   繁体   English

是Thread.sleep(1000); 可靠的东西使用?

[英]Is Thread.sleep(1000); something reliable to use?

I know that Thread.sleep(1000); 我知道Thread.sleep(1000); can be used to freeze the current thread (for 1 second in the example), but since you need to throw an Exception I am wondering if it is safe to use it. 可用于冻结当前线程(在示例中为1秒),但由于您需要抛出Exception我想知道它是否可以安全使用它。 I don't want to use it in a program and accidentally cause problems. 我不想在程序中使用它并意外地导致问题。

Is it "ok" to use Thread.sleep(); 使用Thread.sleep();是否“正常” Thread.sleep(); or is there a better way to achieve the same outcome? 或者有更好的方法来实现相同的结果吗?

As an aside, if you want to sleep and know you won't be using interrupts, a handy way to avoid dealing with the InterruptedExceptions is using Guava's Uninterruptibles, eg 顺便说一句,如果你想睡觉并且知道你不会使用中断,那么避免处理InterruptedExceptions的方便方法是使用Guava的Uninterruptibles,例如

Uninterruptibles.sleepUninterruptibly(1000, TimeUnit.MILLISECONDS);

This transparently deals with the InterruptedException if one is thrown in the recommended way (ie resetting the interrupted status) and avoids unsightly try-catch blocks. 如果以推荐的方式抛出一个(即重置中断状态)并避免难看的try-catch块,这将透明地处理InterruptedException。

since you need to throw an Exception I am wondering if it is safe to use it. 因为你需要抛出异常,我想知道它是否安全使用它。

Thread#sleep only throws 2 exceptions : 线程#sleep仅抛出2个异常

  • IllegalArgumentException if the parameter is negative 如果参数为负,则为IllegalArgumentException
  • InterruptedException if the thread gets interrupted InterruptedException ,如果线程被中断

=> if you pass a positive number and the thread doesn't get interrupted there will be no exception. =>如果传递正数并且线程没有被中断,则不会有异常。

Unless you call theThread.interrupt() from another thread you will be fine. 除非你从另一个线程调用theThread.interrupt() ,否则你会没事的。

EDIT 编辑

It seems you want to create a timer - in that case you would make your life much simpler by using the built-in mechanisms : 看起来你想要创建一个计时器 - 在这种情况下,你可以通过使用内置机制让你的生活更简单:

Runnable r = new Runnable() {
    public void run() { runYourTaskHere(); }
};
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(r, 0, 1, TimeUnit.SECONDS);

Depends on what you are trying to do, I have seen Thread.sleep used and it is usually used to fix a programming mistake, because people think it resolves race conditions. 取决于你想要做什么,我已经看到Thread.sleep被使用,它通常用于修复编程错误,因为人们认为它解决了竞争条件。 Thread.sleep has to throw an exception because Java doesn't control the CPU (last I checked), so if the CPU has a bug or even if it doesn't and wakes a thread up early / interrupts it in some way the program (Java) needs a way to terminate itself and propagate a message up the stack. Thread.sleep必须抛出一个异常,因为Java不控制CPU(最后我检查过),所以如果CPU有错误,或者即使它没有,并提前唤醒一个线程/以某种方式中断它的程序(Java)需要一种方法来终止自身并将消息传播到堆栈中。 Further this greatly depends on what you mean by "the same outcome" 此外,这在很大程度上取决于你的意思“相同的结果”

It is perfectly safe. 这是非常安全的。 InterruptedException has been provided by JVM so that you can handle it if needed ( suppose you want to do something extra when this thread is interrupted)... It actually doesnt mean that something bad has happened. JVM已经提供了InterruptedException,以便您可以在需要时处理它(假设您希望在此线程被中断时执行额外操作)...它实际上并不意味着发生了一些不好的事情。

Since thread.sleep Suspends the current thread for a specified time (not kill it). 由于thread.sleep暂停当前​​线程一段时间(不杀死它)。 So it's safe to use it. 所以使用它是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM