博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
appium中driver.wait报IllegalMonitorStateException的解释
阅读量:4981 次
发布时间:2019-06-12

本文共 1782 字,大约阅读时间需要 5 分钟。

在写appium代码的时候,有的人想使用wait方法,写成:driver.wait(),结果抛出异常:IllegalMonitorStateException,看了appium client的api文档,关于wait方法是这么写的:

 

public final void wait()                throws
Causes the current thread to wait until another thread invokes the   method or the   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 monitor to wake up either through a call to the notify method or thenotifyAll 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 are possible, and this method should always be used in a loop:

synchronized (obj) {         while (
) obj.wait(); ... // Perform action appropriate to condition }
This method should only be called by a thread that 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:
 - if the current thread is not the owner of the object's monitor.
 - if any thread interrupted the current thread before or while the current thread was waiting for a notification. The 
interrupted status of the current thread is cleared when this exception is thrown.
See Also:
IllegalMonitorStateException异常原因:if the current thread is not the owner of the object's monitor,意思就是当前线程不是该对象锁的所有者。
 wait()方法是通知当前线程等待并释放对象锁,要想用wait方法,当前线程需要获取到driver对象锁, 所以需要先获取到对象锁,使用把wait方法放到synchronized块中:

synchronized (driver)

{
driver.wait();
}

这样就可以了,但是需要调用notify方法唤醒线程

转载于:https://www.cnblogs.com/Eric-zhao/p/5193596.html

你可能感兴趣的文章
PHP--------TP中的ajax请求
查看>>
sync framework参考收集系列
查看>>
PHP-----正则表达式
查看>>
spring中bean生命周期
查看>>
Java Service Provider Interface
查看>>
对象的生命周期
查看>>
【DL】模型蒸馏Distillation
查看>>
iOS:为什么TCP连接要三次握手,四次挥手
查看>>
将博客搬至CSDN
查看>>
Mysql查询某字段重复值并删除重复值
查看>>
使用python获取微医数据
查看>>
使用pyinstaller 打包python程序
查看>>
ubuntu 上开发.netcore
查看>>
小程序索引列表排序
查看>>
vue使用video.js解决m3u8视频播放格式
查看>>
前端H5与安卓和ios之间通信
查看>>
7-7
查看>>
knn 数字识别
查看>>
dataframe
查看>>
股票分析
查看>>