Asked 5 years, 4 months ago
Viewed 56k time
41
2

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();
}
asked Jun 12 '14 at 13:38
Erkan Erol

65411 gold badge77 silver badges2323 bronze badges
 

2 Answers

81
 

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

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

But this will throw the exception;

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

Reference;

answered Jun 12 '14 at 13:48
Rudi Kershaw

7,72555 gold badges3838 silver badges6969 bronze badges
 
2

I had the same error, but (for me) the answer suggested by Rudi Kershaw wasn't the issue... I called the notify() of a Notification the wrong way (see the last line of both snippets):

Not working:

public void update() {
mBuilder.setSmallIcon(R.drawable.ic_launcher)
.setPriority(AesPrefs.getInt(R.string.PRIORITY_NOTIFICATION_BATTERY, NotificationCompat.PRIORITY_MAX))
.setOngoing(true);
mBuilder.setWhen(AesPrefs.getLong(Loader.gStr(R.string.LAST_FIRED_BATTERY_NOTIFICATION) + Const.START_CLIPBOARD_NOTIFICATION_DELAYED, -1));
mManager.notify(); // <- lil' mistake
}

Working:

public void update() {
mBuilder.setSmallIcon(R.drawable.ic_launcher)
.setPriority(AesPrefs.getInt(R.string.PRIORITY_NOTIFICATION_BATTERY, NotificationCompat.PRIORITY_MAX))
.setOngoing(true);
mBuilder.setWhen(AesPrefs.getLong(Loader.gStr(R.string.LAST_FIRED_BATTERY_NOTIFICATION) + Const.START_CLIPBOARD_NOTIFICATION_DELAYED, -1));
mManager.notify(Const.NOTIFICATION_CLIPBOARD, mBuilder.build()); // <- ok ;-)
}
answered Dec 29 '15 at 20:19
Martin Pfeffer

8,59877 gold badges4444 silver badges6060 bronze badges
 
 
 
 
https://stackoverflow.com/questions/24185921/object-not-locked-by-thread-before-notify-in-onpostexecute

最新文章

  1. C#得到某月最后一天晚上23:59:59和某月第一天00:00:00
  2. Python基础篇【第5篇】: Python模块基础(一)
  3. 驱动开发学习笔记. 0.07 Uboot链接地址 加载地址 和 链接脚本地址
  4. CSS:CSS定位和浮动
  5. Ubuntu 12.04 Desktop使用XAMPP
  6. Represent nil with NSNull
  7. Jquery 根据value值设置下拉列表(select)默认选中项
  8. poj3177--Redundant Paths(边的双连通)
  9. 15,EasyNetQ-高级API
  10. python一些语法糖用法
  11. oracle的用户管理
  12. C# MediaHelper
  13. 从MongoDB里面取得json格式的数据,然后存为本地的json文件,然后再从json读取变为dict
  14. JS 验证字符串是否为空
  15. python_10 迭代器和生成器
  16. Deep Learning(深度学习)学习笔记整理系列之(一)(转)
  17. ES3之cookie
  18. 【sping揭秘】9、容器内部事件发布(二)
  19. JavaSript模块规范 - AMD规范与CMD规范介绍[转]
  20. 【SQL】MaxComputer常用SQL与注意小结

热门文章

  1. mysql随机取一条记录
  2. 使用量产工具合并U盘空间一例
  3. Netty学习--第一章 JDK自带的BIO
  4. Excel: assign label to scatter chart using specific cell values
  5. react native 实现TODO APP
  6. DC.p4: programming the forwarding plane of a data-center switch
  7. 170906-MyBatis续
  8. VMware 15 搭建MacOS 10.14教程
  9. [CSP-S模拟测试]:计数(DP+记忆化搜索)
  10. 转:SpringMVC常见面试题总结(超详细回答)