美文网首页
handler全家桶

handler全家桶

作者: EdwdChen | 来源:发表于2017-01-29 12:48 被阅读5次

handler是android中实现线程之间通信的方式之一,他的原理值得我们了解。

handler的通信实现原理离不开MessageQueue,Message和Looper

当handler被创建的时候,会检查其内部引用的mLooper对象是否已经存在。

mLooper= Looper.myLooper();

if(mLooper==null) {

throw newRuntimeException(

"Can't create handler inside thread that has not called Looper.prepare()");

}

ThreadLocal是一种以当前thread为key的HashMap,能够将线程间的对象分离开来

public T get() {

Thread t = Thread.currentThread();

ThreadLocalMap map = getMap(t);

if(map !=null) {

ThreadLocalMap.Entry e = map.getEntry(this);

if(e !=null)

return(T)e.value;

}

return setInitialValue();

}

public void set(T value) {

Thread t = Thread.currentThread();

ThreadLocalMap map = getMap(t);

if(map !=null)

map.set(this,value);

else

createMap(t,value);

}

looper以threadlocal的形式存在于thread当中

调用looper.prepare的时候:

private static void prepare(boolean quitAllowed) {

if(sThreadLocal.get() !=null) {

throw newRuntimeException("Only one Looper may be created per thread");

}

sThreadLocal.set(newLooper(quitAllowed));

}

创建looper的时候会一同创建messageQueue:

private Looper(boolean quitAllowed) {

mQueue=new MessageQueue(quitAllowed);

mThread= Thread.currentThread();

}

在looper的loop方法中不断读取messageQueue的msg

代码:

public static void loop() {

finalLooper me =myLooper();

if(me ==null) {

throw newRuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");

}

finalMessageQueue 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 longident = 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

finalPrinter logging = me.mLogging;

if(logging !=null) {

logging.println(">>>>> Dispatching to "+ msg.target +" "+

msg.callback +": "+ msg.what);

}

final longtraceTag = me.mTraceTag;

if(traceTag !=0&& Trace.isTagEnabled(traceTag)) {

Trace.traceBegin(traceTag,msg.target.getTraceName(msg));

}

try{

msg.target.dispatchMessage(msg);

}finally{

if(traceTag !=0) {

Trace.traceEnd(traceTag);

}

}

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 longnewIdent = 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();

}

}

相关文章

  • handler全家桶

    handler是android中实现线程之间通信的方式之一,他的原理值得我们了解。 handler的通信实现原理离...

  • Android | 异步消息处理机制(源码分析+面试题)

    参考文献: Android异步消息处理机制源码剖析 Handler全家桶之 —— Handler 源码解析 你真的...

  • Handler全家桶之 —— Handler 源码解析

    前言 好记性不如烂笔头。 这是一个系列文章,将会包括: Handler全家桶之 —— Handler 源码解析 H...

  • [南邮OJ]密码学

    base64全家桶 全家桶全家桶全家桶!我怎么饿了。。。。。。密文(解密前删除回车):R1pDVE1NWlhHUT...

  • (五)React-router路由

    ?React全家桶? React全家桶地址React全家桶(一)之React入门?https://blog.csd...

  • 一文读懂 Handler 机制全家桶

    Handler 在整个 Android 开发体系中占据着很重要的地位,对开发者来说起到的作用很明确,就是为了实现线...

  • 一文读懂 Handler 机制全家桶

    Handler 在整个 Android 开发体系中占据着很重要的地位,对开发者来说起到的作用很明确,就是为了实现线...

  • [南邮OJ](密码学)base64全家桶

    题目链接: base64全家桶 150全家桶全家桶全家桶!我怎么饿了。。。。。。密文(解密前删除回车):R1pDV...

  • 全家桶

    React 全家桶1.react主体2.webpack:grunt/gulp 自动化构建工具3.flex 布局4....

  • 全家桶

    今天发了“全家桶”,在等待结果那一刻,即忐忑,又激动,终于是一道杠,这才放下绷紧的心。 小区已经封了近20天了,都...

网友评论

      本文标题:handler全家桶

      本文链接:https://www.haomeiwen.com/subject/tchzbttx.html