为什么Thread.sleep使用不好 [英] Why Thread.sleep is bad to use

查看:173
本文介绍了为什么Thread.sleep使用不好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对这个重复的问题道歉,但我还没有找到任何令人满意的答案。大多数问题都有自己的特定用例:

Java - 替代thread.sleep

有没有更好的或替代的方法来跳过/避免在Java中使用Thread.sleep(1000)?

Apologies for this repeated question but I haven't found any satisfactory answers yet. Most of the question had their own specific use case:
Java - alternative to thread.sleep
Is there any better or alternative way to skip/avoid using Thread.sleep(1000) in Java?

我的问题是非常通用的用例。等待条件完成。做一些操作。检查条件。如果条件不成立,请等待一段时间再进行相同的操作。

My question is for the very generic use case. Wait for a condition to complete. Do some operation. Check for a condition. If the condition is not true, wait for some time and again do the same operation.

例如考虑一种通过调用其createAPI表来创建DynamoDB表的方法。 DynamoDB表需要一些时间才能变为活动状态,因此该方法会调用其DescribeTable API以定期轮询状态直到某个时间(假设5分钟 - 由于线程调度而导致的偏差是可接受的)。如果表在5分钟内变为活动状态,则返回true,否则抛出异常。

For e.g. Consider a method that creates a DynamoDB table by calling its createAPI table. DynamoDB table takes some time to become active so that method would call its DescribeTable API to poll for status at regular intervals until some time(let's say 5 mins - deviation due to thread scheduling is acceptable). Returns true if the table becomes active in 5 mins else throws exception.

这是伪代码:

public void createDynamoDBTable(String name) {
  //call create table API to initiate table creation

  //wait for table to become active
  long endTime = System.currentTimeMillis() + MAX_WAIT_TIME_FOR_TABLE_CREATE;

  while(System.currentTimeMillis() < endTime) {
    boolean status =  //call DescribeTable API to get status;
    if(status) {
         //status is now true, return
         return
    } else {
        try {
            Thread.sleep(10*1000);
        } catch(InterruptedException e) {
        }
    }
  }

  throw new RuntimeException("Table still not created");
}

我理解通过使用 Thread.sleep 阻塞当前线程,从而消耗资源。但是在一个相当中等规模的应用程序中,一个线程是一个大问题吗?

我读到的地方使用 ScheduledThreadPoolExecutor 并在那里进行状态轮询。但同样,我们必须使用至少一个线程初始化此池,其中运行的runnable方法将运行。

I understand that by using Thread.sleep blocks the current thread, thereby consuming resources. but in a fairly mid size application, is one thread a big concern?
I read somewhere that use ScheduledThreadPoolExecutor and do this status polling there. But again, we would have to initialize this pool with at least 1 thread where runnable method to do the polling would run.

有关使用 Thread.sleep 据说是个坏主意,实现上述目的的替代方案有哪些。

Any suggestions on why using Thread.sleep is said to be such a bad idea and what are the alternative options for achieving same as above.

http ://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx

推荐答案

在这种情况下使用 Thread.sleep 可以。人们不鼓励 Thread.sleep 的原因是因为它常常用于修复竞争条件,用于基于通知的同步是一个更好的选择等。

It's fine to use Thread.sleep in that situation. The reason people discourage Thread.sleep is because it's frequently used in an ill attempt to fix a race condition, used where notification based synchronization is a much better choice etc.

在这种情况下,AFAIK您没有选项但是投票,因为API没有为您提供通知。我也可以看到这是一个不经常的操作,因为大概你不会创建千表。

In this case, AFAIK you don't have an option but poll because the API doesn't provide you with notifications. I can also see it's a infrequent operation because presumably you are not going to create thousand tables.

因此,我觉得在这里使用 Thread.sleep 很好。如你所说,当你要阻止当前线程时,产生一个单独的线程似乎会使事情变得复杂而没有任何价值。

Therefore, I find it fine to use Thread.sleep here. As you said, spawning a separate thread when you are going to block the current thread anyways seems to complicate things without merit.

这篇关于为什么Thread.sleep使用不好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆