When writing Appium code, some people want to use the Wait method, written as: Driver.wait (), the result throws an exception: Illegalmonitorstateexception, see the Appium Client API documentation, The wait method is written like this:
Public final void Wait () throws Interruptedexception
Causes the current thread to wait until another thread invokes the
notify()
Method or the
notifyAll()
Method for this object. In other words, this method behaves exactly as if it simply performs the call
wait(0)
.
The current thread must own this object ' s monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object ' s Mon Itor to wake up either through a call to the notify
method or the notifyAll
method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups is possible, and this method should always being used in a l Oop:
Synchronized (obj) { while (<condition does not hold>) obj.wait (); ...//Perform action appropriate to condition
This method, should only being called by a thread, is the owner of this object ' s monitor. See the
notify
Method for a description of the ways in which a thread can become the owner of a monitor.
- throws:
-
illegalmonitorstateexception
-if the CU Rrent thread is not the owner of the object ' s monitor.
-
interruptedexception
-If any thread interrupted the current thread before or while the CU Rrent thread is waiting for a notification. the
interrupted status of the current thread was cleared when this exception is thrown.
- see Also:
-
notify ()
,
notifyall ()
-
-
-
illegalmonitorstateexception Exception Reason: If the current thread was not the owner of the OBJEC T ' s monitor, meaning that the current thread is not the owner of the object lock. The
-
wait () method is to notify the current thread to wait and release the object lock, and to use the Wait method, the current thread needs to get to the driver object lock, so the object lock needs to be acquired first. Use the Wait method in the synchronized block:
Synchronized (Driver)
{
driver.wait ();
}
This is possible, but you need to call the Notify method to wake the thread
Explanation of driver.wait in Appium illegalmonitorstateexception