Object not locked by thread before notify() in onPostExecute
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();
}
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;
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 ;-)
}
Object not locked by thread before notify() in onPostExecute的更多相关文章
- notification:object not locked by thread before notify()
今天写notification练习时,误将NotificationManager.notify(0, notification);写成notification.notify(); 代码如下 publi ...
- Java Thread wait, notify and notifyAll Example
Java Thread wait, notify and notifyAll Example Java线程中的使用的wait,notify和nitifyAll方法示例. The Object clas ...
- [译]Java Thread wait, notify和notifyAll示例
Java Thread wait, notify和notifyAll示例 Java上的Object类定义了三个final方法用于不同线程间关于某资源上的锁状态交互,这三个方法是:wait(), not ...
- Object的wait和Thread的sleep
Object的wait() wait()搭配notify(),nofityAll()使用. 线程获取到对象锁之后,执行wait()就会释放对象锁,同时线程挂起,直到其他线程获取到对象锁并执行notif ...
- 为什么等待和通知是在 Object 类而不是 Thread 中声明的?
一个棘手的 Java 问题,如果 Java编程语言不是你设计的,你怎么能回答这个问题呢.Java编程的常识和深入了解有助于回答这种棘手的 Java 核心方面的面试问题.为什么 wait,notify ...
- AttributeError: 'module' object has no attribute 'Thread'
$ python thread.py starting at: 2015-08-05 00:24:24Traceback (most recent call last): File "th ...
- 'module' object has no attribute 'Thread'解决方法及模块加载顺序
源码片段: class myThread(threading.Thread): def __init__(self, threadID, name, counter): threading.Threa ...
- Thread线程notify方法的自我理解
感谢博主:http://zy19982004.iteye.com/blog/1626916 这篇博文给予我线程知识很大的帮助 知识背景:(1)wait().notify()均是Object的方法,故每 ...
- Thread wait notify sleep
wait: 必须暂定当前正在执行的线程,并释放资源锁,让其他线程可以有机会运行 notify/notifyall: 唤醒因锁池中的线程,使之运行 wait与sleep区别 对于sleep()方法,我们 ...
随机推荐
- es5继承和es6类和继承
es6新增关键字class,代表类,其实相当于代替了es5的构造函数 通过构造函数可以创建一个对象实例,那么通过class也可以创建一个对象实列 /* es5 创建一个person 构造函数 */ f ...
- unittest加载用例
diascover加载测试用例 1.discover方法里面有三个参数: -case_dir:这个是待执行用例的目录. -pattern:这个是匹配脚本名称的规则,test*.py意思是匹配test开 ...
- nginx之热部署,以及版本回滚
热部署的概念:当从老版本替换为新版本的nginx的时候,如果不热部署的话,会需要取消nginx服务并重启服务才能替换成功,这样的话会使正在访问的用户在断开连接,所以为了不影响用户的体验,且需要版本升级 ...
- codeforces 108D Basketball Team(简单组合)
D. Basketball Team time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 4--面试总结-promise
promise异步原理: 定义:promise是异步编程的解决方案,可以解决异步回调地狱的问题: 原理:三种状态两种结果的一个状态机:三种状态(pending,fulfilled,rejected)两 ...
- Zookeeper学习笔记(中)
Zookeeper学习笔记(中) Zookeeper的基本原理和基本实现 深入了解ZK的基本原理 ZK的一致性: ZAB 协议: Zookeeper 原子消息广播协议 ZK通过选举保证 leader ...
- Django【第26篇】:中介模型以及优化查询以及CBV模式
中介模型以及优化查询以及CBV模式 一.中介模型:多对多添加的时候用到中介模型 自己创建的第三张表就属于是中介模型 class Article(models.Model): ''' 文章表 ''' t ...
- Git回滚到指定的commit
查看历史commint $ git log (可以记下sha码) 回退命令: $ git reset --hard HEAD^ 回退到上个版本$ git reset --hard HEAD~3 回退到 ...
- 初识Django之前端后端与数据库的配置
目录 一.静态文件配置 二.关于form表单的补充 三.post请求出现403问题 四.request方法初识 4.1判断当前请求方式 4.2request.POST与request.GET 五.py ...
- 理解URL以及如何区分相对URL和绝对URL
URL(Uniform Resource Locator 统一资源定位符)可以理解为网络地址. url 包含了关于文件储存位置和浏览器应该如何处理文件的信息. URL的第一个部分称为模式scheme, ...