java - 在 onPostExecute 中的 notify() 之前没有被线程锁定的对象

标签 java android multithreading

我尝试在 onPostExecute 中通知适配器主类的 ListView ,但我收到错误:java.lang.IllegalMonitorStateException:object not locked by thread before notify()

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

最佳答案

.notify() 方法必须从 synchronized 上下文中调用,即从 synchronized block 内调用。

java.lang.IllegalMonitorStateException当您在一个未用作您调用 notify 的同步块(synchronized block)的锁的对象上调用 .notify() 时,将引发此错误。例如,以下工作;

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();        
}

引用;

关于java - 在 onPostExecute 中的 notify() 之前没有被线程锁定的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24185921/

相关文章:

java - Gson 输出 com.google.gson.JsonSyntaxException : java. io.EOFException:第 1 行第 501 列输入结束

java - 如何设置图片大小正方形.. 所有手机屏幕都支持哪些?

python - Python 中的线程 block Main

C++ 等待所有线程完成

java - 如何从 LinkedList pp = new LinkedList(); 中删除整数?

java - 为 HTTPS 连接设置 https.protocols 系统属性的问题

java - 编译正常时运行时出现 NoClassDefFoundError

java - 从 xy 移动到另一点,我在时间 t 在哪里?

java - 错误:The project was not built since its build path is incomplete

java - RabbitMQ 跨多线程多消费者负载均衡