HandlerThread使用总结
来源:互联网 发布:nothing软件怎么样 编辑:程序博客网 时间:2024/09/21 06:47
- HandlerThread实际上是一个Thread,但是在其内部却创建了Looper和MessageQueue等对象。
@Override public void run() { mTid = Process.myTid(); Looper.prepare(); synchronized (this) { mLooper = Looper.myLooper(); notifyAll(); } Process.setThreadPriority(mPriority); onLooperPrepared(); Looper.loop(); mTid = -1; }run方法里面mLooper创建完成后有个notifyAll(),getLooper()中有个wait(),这是为什么?因为mLooper在一个线程中执行,而handler实在UI线程初始化的,也就是说必须等mLooper创建完成,才能正确返回getLooper()。wait(),notify()就是为了解决这两个线程的同步问题。
通过HandlerThread不但可以实现UI线程与子线程的通信同样也可以实现子线程之间的通信
HandlerThread在不需要使用的时候需要手动的回收掉
HandlerThread mHandlerThread = new HandlerThread("<TAG>");
mHandlerThread.start();
Handler mHandler = new Handler(mHandlerThread.getLooper()) {
public void handleMessage(Message mes) {
}}
mHandlerThread.quit() //exit the HandlerThread
0 0
- Android HandlerThread使用总结
- Android HandlerThread使用总结
- HandlerThread使用总结
- Android HandlerThread 总结使用
- Android HandlerThread 总结使用
- Android HandlerThread 总结使用
- Android HandlerThread 总结使用
- Android HandlerThread总结使用
- Android HandlerThread 总结使用
- Android HandlerThread 总结使用
- HandlerThread 总结
- Android的HandlerThread的使用总结
- HandlerThread使用
- Android Handler、Loop 的简单使用 和 HandlerThread 总结使用
- Android-Handler 总结(Handler 的使用 ,Looper , MessageQueue , HandlerThread )
- Handler,Looper,Message,MessageQueue,HandlerThread使用总结(上)
- Android之使用HandlerThread 以及如何退出总结
- Android-Handler 总结(Handler 的使用 ,Looper , MessageQueue , HandlerThread )
- Retrofit初体验
- 完全搞定iframe(框架)里的滚动条
- Java 集合框架源码分析(三)——HashMap
- Android Material Design系列之CollapsingToolbarLayout
- 我的spark学习之路(一)
- HandlerThread使用总结
- 23种设计模式C++实例之观察者模式
- CodeForces 589B -- B. Layer Cake(多重集合+技巧枚举)
- 2016多校训练Contest7: 1004 Distance hdu5812
- qml 实现图片旋转, 且鼠标可以拖动图片,鼠标点击在两幅图片间切换
- bjfu1004 Sudoku
- 计算几何 graham 最大凸包
- ORA-01795:列表中的最大表达式数为1000
- 排序算法