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()方法,我们 ...
随机推荐
- python数据结构:pandas(3)
一.pandas数据操作: 1.处理缺失数据 (1)判断是否存在缺失值 ser_obj.isnull(),df_obj.isnull() (2)dropna:丢弃缺失数据 (3)fillna:填充缺失 ...
- vue项目1-pizza点餐系统11-设计menu页面
菜单的页面设计是基于bootstrap实现的,主要用到的是table标签,其中获取data数据用到遍历. <template> <div class=""> ...
- CSS中的伪类和为伪元素
伪类: 伪元素:
- GROUP BY关键字优化
1.group by实质是先排序后进行分组,遵照索引建的最佳左前缀 2.当无法使用索引列,增大max_length_for_sort_data参数的设置+增大sort_buffer_size参数的设置 ...
- C# List<Object>值拷贝
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Run ...
- mock.js模拟ajax数据请求
在我们开发过程中存在着前端页面ui完成了,但是没有接口进行联调数据的情况,现在介绍一下用mock.js来完成数据的请求.这样在后期我们的后台接口完成后只需要更改请求的接口名字即可!前提是你的模拟字段名 ...
- CSP-S 赛前模板复习
快读模板 这个连算法都算不上... inline int read() { int x=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9') ...
- php 获取真实ip
REMOTE_ADDR:是和服务器直接"握手"的IP. HTTP_CLIENT_IP:代理服务器添加的 HTTP 头,存放客户端真实IP. HTTP_X_FORWARDED_FOR ...
- 【ARC101F】Robots and Exits 树状数组优化DP
ARC101F Robots and Exits 树状数组 有 $ n $ 个机器人和 $ m $ 个出口.这 $ n $ 个机器人的初始位置是 $ a_1,a_2.....a_n $ ,这 $ m ...
- Django【第16篇】:Django之Form组件自定义验证规则
自定义验证规则以及中间件简单介绍 1.python2和python3中的区别 对于python2内置的字符串类型有str和unicode 比如:"abc"是字符串,u"你 ...
android
