zoukankan      html  css  js  c++  java
  • appium中driver.wait报IllegalMonitorStateException的解释

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

     

    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 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 (<condition does not hold>)
                 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:
    IllegalMonitorStateException - if the current thread is not the owner of the object's monitor.
    InterruptedException - 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:
    notify()notifyAll()
    IllegalMonitorStateException异常原因:if the current thread is not the owner of the object's monitor,意思就是当前线程不是该对象锁的所有者。
     wait()方法是通知当前线程等待并释放对象锁,要想用wait方法,当前线程需要获取到driver对象锁, 所以需要先获取到对象锁,使用把wait方法放到synchronized块中:

    synchronized (driver)
    {
    driver.wait();
    }

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

  • 相关阅读:
    2020年12月学习记录
    Data Protection Application Programming Interface滥用攻击
    另类的缓存凭证收集
    js获取地址栏参数,携带参数跳转页面
    用JS获取地址栏参数的方法
    解决vue加载时闪烁
    vue-cli-service build 不同环境配置
    vite创建vue3.x项目报404的解决方案
    后端开发完接口才给出接口文档,合理吗?
    API研发实现规范化管理的价值
  • 原文地址:https://www.cnblogs.com/Eric-zhao/p/5193596.html
Copyright © 2011-2022 走看看