What are some reasons why writing the following piece of code is considered bad practice?
while (someList.isEmpty()) {
try {
Thread.currentThread().sleep(100);
}
catch (Exception e) {}
}
// Do something to the list as soon as some thread adds an element to it.
To me, picking an arbitrary value to sleep is not good practice, and I would use a BlockingQueue
in this situation, but I'd like to know if there is more than one reason why one shouldn't write such code.
It imposes an average delay of 50 milliseconds before the event is acted on, and it wakes up 10 times a second when there's no event to handle. If neither of those things particularly matter, then it's just inelegant.