Notes on condition Synchronization in Java
Notes on condition Synchronization in Java
- Waiting in a loop
- Since the thread calling notify() may continue and invalidate the condition signalled, it is safer to always use the idiom:while(!B) wait();where B is the condition that the thread awaits to be true.
- Interrupted Exception
- InterruptedException is a static exception which can be thrown by wait(). It must either be caught by a method using wait()or explicitly declared as being thrown by the method (throws). To avoid cluttering up the example code with exception handling code, we will always catch and ignore the exception immediately: try {wait();} catch (InterruptedException e){}