Describe the environment in which a wait call is legalSolut
Describe the environment in which a wait () call is legal?
Solution
Object.wait() call must be placed in synchronized block, otherwise an IllegalMonitorStateException is thrown.
You can call the wait method like the following:-
The Wait() method does the following, in order:
This means that despite appearances, no lock is held on the synchronization object while Monitor.Wait awaits a pulse:
lock (object){
while (!_go)
Monitor.Wait (object); // _lock is released
// lock is regained
}
Execution then continues at the next statement. Monitor.Wait is designed for use within a lock statement; it throws an exception if called otherwise.
