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. 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
. Ignore this IDE warning will be my last option.
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.