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()方法,我们 ...
随机推荐
- numpy.random.randn()和numpy.random.rand()
1 numpy.random.rand() (1)numpy.random.rand(d0,d1,…,dn) rand函数根据给定维度生成[0,1)之间的数据,包含0,不包含1 dn表格每个维度 返回 ...
- 原生js格式化json和格式化xml的方法
在工作中一直看各位前辈的博客解决各种问题,对我的帮助很大,非常感谢! 之前一直比较忙没有写博客,终于过年有了点空闲时间,可以把自己积累的东西分享下,笔记中的部分函数不是自己写的,都是工作中一点点积累的 ...
- Python安装模块包
可以利用pycharm安装模块包 使用这种方法安装时,可能会报下面类型的异常 AttributeError: module 'pip' has no attribute 'main' 出现这这样的异常 ...
- 通过关键字Event定义用户自己的事件
Event 语句 定义用户自定义的事件. 语法[Public] Event procedurename [(arglist)] Event 语句包含下面部分: 部分 描述 Public 可选的.指定该 ...
- Beta冲刺-(2/3)
这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/ 这个作业要求在哪里 https://edu.cnbl ...
- 7天玩转性能&接口测试
众所周知,近10年IT领域有两个关键的风向转变,传统IT向云计算转变,传统瀑布和迭代开发模式向敏捷开发模式转变.这两个转变促成了DevOps产品交付模式的出现.互联网行业竞争激烈,许多公司专注于产品和 ...
- Java验证码程序
1.设计思想利用random的随机生成数字,利用for循环控制随机数字的个数来控制验证码的输出.利用JFrame实现布局的管理,对登录框内容的位置进行管理. 2.流程图 3.源代码 denglu类 i ...
- ESP8266-01
我的模块购买地址 https://buyertrade.taobao.com/trade/detail/tradeSnap.htm?tradeID=460212770243341548&sn ...
- Jackson常用工具类
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11983194.html Demo package org.fool.util; import com. ...
- postman-鉴权
概念 Cookie和鉴权的区别,cookie一般指缓存在本地的数据:鉴权一般指验证用户是否拥有访问系统的权利 鉴权分类 Basic auth:基础鉴权,数据没有加密可明文显示,一般在测试环境使用,不在 ...
android
