HandlerThread 的用法 和Thread 的区别
来源:互联网 发布:模拟器软件闪退 编辑:程序博客网 时间:2024/05/16 05:53
HandlerThread 继承了Thread 所以 具有Thread的特性,他的不同之处在哪里那?请看源码:
HandlerThread常在什么情况下使用那?
@Override public void run() { mTid = Process.myTid(); Looper.prepare(); synchronized (this) { mLooper = Looper.myLooper(); notifyAll(); } Process.setThreadPriority(mPriority); onLooperPrepared(); Looper.loop(); mTid = -1; }
可以在run 方法中可以看到创建了这个线程的looper ,looper的构造函数中创建了唯一的消息队列MessageQueue, 每个线程只可能有唯一的looper 在Looper.loop()可以看到有个循环体,这个循环体就是不断从队列中取消息分发消息。
/** * Run the message queue in this thread. Be sure to call * {@link #quit()} to end the loop. */ public static void loop() { final Looper me = myLooper(); if (me == null) { throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread."); } final MessageQueue queue = me.mQueue; // Make sure the identity of this thread is that of the local process, // and keep track of what that identity token actually is. Binder.clearCallingIdentity(); final long ident = Binder.clearCallingIdentity(); for (;;) { Message msg = queue.next(); // might block if (msg == null) { // No message indicates that the message queue is quitting. return; } // This must be in a local variable, in case a UI event sets the logger Printer logging = me.mLogging; if (logging != null) { logging.println(">>>>> Dispatching to " + msg.target + " " + msg.callback + ": " + msg.what); } msg.target.dispatchMessage(msg); if (logging != null) { logging.println("<<<<< Finished to " + msg.target + " " + msg.callback); } // Make sure that during the course of dispatching the // identity of the thread wasn't corrupted. final long newIdent = Binder.clearCallingIdentity(); if (ident != newIdent) { Log.wtf(TAG, "Thread identity changed from 0x" + Long.toHexString(ident) + " to 0x" + Long.toHexString(newIdent) + " while dispatching to " + msg.target.getClass().getName() + " " + msg.callback + " what=" + msg.what); } msg.recycleUnchecked(); } }
HandlerThread常在什么情况下使用那?
重复使用线程来处理任务时,我们没有必要重复的创建thread 这样会印象程序的性能,这种情况尽量使用HandlerThread
HandlerThread thread = new HandlerThread("MyHandlerThread"); thread.start(); mHandler = new myHandler(thread.getLooper()); mHandler.sendEmptyMsg(0);
0 0
- HandlerThread 的用法 和Thread 的区别
- Handler+Thread和HandlerThread的用法比较
- Handler+Thread和HandlerThread的用法比较
- Handler、HandlerThread、Thread的区别
- Thread、Handler、HandlerThread的区别
- Handler、Thread和HandlerThread的差别
- Handler、Thread和HandlerThread的差别
- Android HandlerThread的用法
- HandlerThread的用法
- Android HandlerThread的用法
- HandlerThread的用法
- Thread,Handler,HandlerThread区别
- android 使用Thread的一种方法-HandlerThread
- Android HandlerThread的简单用法
- HandlerThread更新ui和Log日志的用法
- android中HandlerThread的原理和用法讲解
- Handler,looper,MessageQueue及 handlerThread 之间的关联和用法
- handler与handlerThread的区别
- 在Activity中得到新打开Activity 关闭后返回的数据
- hdu-2112 HDU Today
- ACM-数学知识的算法应用
- HDU3308 LCIS 最长连续上升序列。
- Java出局游戏
- HandlerThread 的用法 和Thread 的区别
- HDU 2063 过山车( 最大匹配 )
- 32位与64位平台
- poj 2528(区间修改+离散化)
- Executor
- VIM命令集合
- java贪吃蛇
- [Java]LeetCode57 Insert Interval
- 机器人(1.1位姿几何基础——刚体位置和姿态描述)