简体   繁体   English

While 循环中的 Thread.sleep()

[英]Thread.sleep() in While Loop

There is a producer keeps generating files and put them into a special path.有一个生产者不断生成文件并将它们放入一个特殊的路径中。 The consumer is a using WatchService to monitor the path and pick up any new generated files to work.消费者是一个使用WatchService来监视路径并选择任何新生成的文件来工作。 Usually, the consumer's speed is slower than producer.通常,消费者的速度比生产者慢。 In rare case, if the consumer finishes all the existing tasks and there is nothing new in the path yet, the consumer needs to hold on for a while.在极少数情况下,如果消费者完成了所有现有任务并且路径中还没有新的东西,消费者需要坚持一段时间。

while(true) {
    // do something...
    if(condition) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // handler
        }
    }
}

IDE is complaining that calling to Thread.sleep() in a while loop are indicative of "busy-waiting". Busy-waiting is often inefficient, and may result in unexpected deadlocks as busy-waiting threads do not release locked resources IDE 抱怨calling to Thread.sleep() in a while loop are indicative of "busy-waiting". Busy-waiting is often inefficient, and may result in unexpected deadlocks as busy-waiting threads do not release locked resources calling to Thread.sleep() in a while loop are indicative of "busy-waiting". Busy-waiting is often inefficient, and may result in unexpected deadlocks as busy-waiting threads do not release locked resources . calling to Thread.sleep() in a while loop are indicative of "busy-waiting". Busy-waiting is often inefficient, and may result in unexpected deadlocks as busy-waiting threads do not release locked resources Ignore this IDE warning will be my last option.忽略这个 IDE 警告将是我的最后选择。

How could I change my code to avoid the above warning architecture wise?我如何更改我的代码以避免上述警告架构明智?

(It seems a scheduled thread pool won't meet my requirement. Please correct me if I'm wrong.) (看来预定的线程池不能满足我的要求。如果我错了,请纠正我。)

Thanks for your comments and answers.感谢您的评论和回答。 Buffer events from WatchService to a blocking queue will work.从 WatchService 到阻塞队列的缓冲事件将起作用。

I completely agree with the IDE.我完全同意 IDE。 If you use Thread.sleep() you are wrong, most of the time.如果你使用 Thread.sleep() 你错了,大多数时候。 If you don't know why, then you are wrong all of the time.如果你不知道为什么,那么你总是错的。 Yeah I know, it's a strong statement, but in my opinion it should always been kept in mind: in the best case "wrong" means "sub-optimal", in worst cases "wrong" can mean "bad architecture".是的,我知道,这是一个强有力的声明,但在我看来,应该始终牢记这一点:在最好的情况下,“错误”意味着“次优”,在最坏的情况下,“错误”可能意味着“糟糕的架构”。

In your case, a solution is to use a blocking queue that sends consumer to sleep when the queue is empty and wakes up sleeping consumers when some element arrives.在您的情况下,一种解决方案是使用阻塞队列,当队列为空时将消费者发送到睡眠状态,并在某些元素到达时唤醒睡眠中的消费者。

By the way, I also believe you have the same issue when the queue is full: you may want to pause the producer.顺便说一句,我也相信当队列已满时你也会遇到同样的问题:你可能想暂停生产者。

Using Thread.sleep in a real time application is not recommended at all.完全不建议在实时应用程序中使用 Thread.sleep。 For your requirement it is recommended to use a scheduler to check up on the path once in a while based on the your application traffic.根据您的要求,建议使用调度程序根据您的应用程序流量不时检查路径。 Any cron job would also help.任何 cron 工作也会有所帮助。

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

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