本文介绍了对象未在onPostExecute中的notify()之前被线程锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在onPostExecute中通知适配器主类列表视图的适配器,但收到错误消息:java.lang.IllegalMonitorStateException:对象在notify()之前没有被线程锁定

I try to notify adapters of listviews of main class in onPostExecute but I receive the error: java.lang.IllegalMonitorStateException:object not locked by thread before notify()

@Override
protected void onPostExecute(String result) {
    popularfragment.adapter.notifyDataSetChanged();
    recentfragment.adapter.notifyDataSetChanged();
}

推荐答案

必须从synchronized上下文内部(即,从synchronized块内部)调用.notify()方法.

The .notify() method has to be called from within a synchronized context, ie from inside a synchronized block.

抛出 java.lang.IllegalMonitorStateException 在未用作调用通知的同步块的锁的对象上调用.notify()时.例如,以下作品;

The java.lang.IllegalMonitorStateException is thrown when you call .notify() on an object that is not used as the lock for the synchronized block in which you call notify. For example, the following works;

synchronized(obj){
    obj.notify();
}

但这会引发异常;

synchronized(obj){
    // notify() is being called here when the thread and
    // synchronized block does not own the lock on the object.
    anotherObj.notify();
}

参考;

  • IllegalMonitorStateException API
  • How to use wait & notify

这篇关于对象未在onPostExecute中的notify()之前被线程锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:10