对象未在onPostExecute中的notify()之前被线程锁定

Erk*_*rol 41 java multithreading android

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

@Override
protected void onPostExecute(String result) {
    popularfragment.adapter.notifyDataSetChanged();
    recentfragment.adapter.notifyDataSetChanged();
} 
Run Code Online (Sandbox Code Playgroud)

Rud*_*haw 81

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

java.lang.IllegalMonitorStateException当您调用.notify()未用作调用notify的synchronized块的锁定的对象时,抛出此异常.例如,以下作品;

synchronized(obj){
    obj.notify();
}
Run Code Online (Sandbox Code Playgroud)

但这将抛出异常;

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

参考;