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

Object not locked by thread before notify() in onPostExecute的更多相关文章

  1. notification:object not locked by thread before notify()

    今天写notification练习时,误将NotificationManager.notify(0, notification);写成notification.notify(); 代码如下 publi ...

  2. Java Thread wait, notify and notifyAll Example

    Java Thread wait, notify and notifyAll Example Java线程中的使用的wait,notify和nitifyAll方法示例. The Object clas ...

  3. [译]Java Thread wait, notify和notifyAll示例

    Java Thread wait, notify和notifyAll示例 Java上的Object类定义了三个final方法用于不同线程间关于某资源上的锁状态交互,这三个方法是:wait(), not ...

  4. Object的wait和Thread的sleep

    Object的wait() wait()搭配notify(),nofityAll()使用. 线程获取到对象锁之后,执行wait()就会释放对象锁,同时线程挂起,直到其他线程获取到对象锁并执行notif ...

  5. 为什么等待和通知是在 Object 类而不是 Thread 中声明的?

    一个棘手的 Java 问题,如果 Java编程语言不是你设计的,你怎么能回答这个问题呢.Java编程的常识和深入了解有助于回答这种棘手的 Java 核心方面的面试问题.为什么 wait,notify ...

  6. AttributeError: 'module' object has no attribute 'Thread'

    $ python thread.py starting at: 2015-08-05 00:24:24Traceback (most recent call last):  File "th ...

  7. 'module' object has no attribute 'Thread'解决方法及模块加载顺序

    源码片段: class myThread(threading.Thread): def __init__(self, threadID, name, counter): threading.Threa ...

  8. Thread线程notify方法的自我理解

    感谢博主:http://zy19982004.iteye.com/blog/1626916 这篇博文给予我线程知识很大的帮助 知识背景:(1)wait().notify()均是Object的方法,故每 ...

  9. Thread wait notify sleep

    wait: 必须暂定当前正在执行的线程,并释放资源锁,让其他线程可以有机会运行 notify/notifyall: 唤醒因锁池中的线程,使之运行 wait与sleep区别 对于sleep()方法,我们 ...

随机推荐

  1. java语法糖:(1)可变长度参数以及foreach循环原理

    语法糖 语法糖:是一种几乎每种语言或多或少都提供过的一些方便程序员开发代码的语法,它只是编译器实现的一些小把戏罢了,编译期间以特定的字节码或者特定的方式对这些语法做一些处理,开发者就可以直接方便地使用 ...

  2. ID 学习二 FILE I/O

    TCP CONNECT 此文件io工具用于打开和另外一台电脑的tcp/ip连接 电脑(本地网络.ip地址或者完整主机名称) Inputs Server ID 想要连接的主机名称或者IP地址 TCP P ...

  3. CF1260C Infinite Fence 题解(扩欧)

    题目地址 CF1260C 题目大意 现有\(10^{100}\)块木板需要涂漆,第x块如果是x是a的倍数,则涂一种颜色,是b的倍数,则涂另一种颜色.如果既是a又是b的倍数,那么两种颜色都可以涂:如果连 ...

  4. 文本检错——中文拼写检查工具FASPell

    最近因为相关项目需要考虑中文文本检错,然后就发现了爱奇艺发布的号称SOTA的FASPell已经开源代码,所以开始着手实现. 检错思想两步:一,掩码语言模型(MLM)产生候选字符:二,CSD过滤候选字符 ...

  5. LeetCode--114--二叉树展开为链表(python)

    给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \  2   5 / \     \ 3 4      6将其展开为: 1  \   2   \    3  \       4  \ ...

  6. HTML页面滑动到最底部触发事件

    其实基本原理做一个判断,如果 页面总高度  =  视口高度  +  浏览器窗口上边界内容高度 ,那么就是把页面滑动到了最低部,然后执行一个事件. //要触发的事件(自己定义事件的内容) functio ...

  7. 微信小程序-坑1-await-async

    VM45:1 thirdScriptError sdk uncaught third Error regeneratorRuntime is not defined ReferenceError: r ...

  8. B/S实现大视频上传

    在公司做B/S 开发与维护三年啦, 对B/S架构的了解也是只知大概,对于这种基础知识还是很有必要理一理哒.趁空去网上查阅了资料,顺便整理一份笔记供以后查询. 一. B/S的概念 B/S(Brower/ ...

  9. Spring——简介

    学习网站: [1]http://spring.io/ [2]http://projects.spring.io/spring-framework/ Spring是为解决企业应用开发的复杂性而创建的,是 ...

  10. 改变icon方向

    例你想要箭头朝上的图标而你只有箭头朝下的图标,不幸的是你又没有朝上的图标,那就旋转图标. <i class="icon iconfont _icon-iconfontfanhui4&q ...