What is the best way to wait for an event/update. For example, I am waiting for this data structure to be updated before doing something. Implementing it inside a loop is not the best way since it consumes much CPU time like:
while (true) {
// keep checking the data structure
// if data structure is updated
// break;
}
// do something here
What’s a simple but efficient way to implement something like this in Java?
wait-notifyAll is more efficient way than loop.
Standard idiom for wait():
But it’s primitive way to control threads, you’d better use classes in
java.util.concurrentpackage. Moreover, I will choose Chris Dail’s answer if I meet such problem.