Android线程切换之谜

作者: 一只小松 | 来源:发表于2017-08-04 19:30 被阅读0次

之前有写过一篇文章 记一次Handler的优化 ,当时并未详细的讲解Handler的线程切换功能。遂写下这篇文章,对其进行补充。
那么这一篇不得的提及到另外一个重要的类Looper。提取了Looper类中几段重要之处进行分析,其实线程切换真的很简单。

public final class Looper{
......
/**
* Run the message queue in this thread. Be sure to call
* {@link #quit()} to end the loop.
*/
public static void loop(){
    // 看方法上的注释也能明白为何线程能够切换,因为handler发送的消息在创建handler关联的Looper对象所在线程被消费
    final Looper me = myLooper();// 获取当前线程对应的Looper对象
    if(me == null){
        throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
    }
    final MessageQueue queue = me.mQueue;// 获取Looper对象中的消息队列
    // 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 阻塞获取Looper队列中的消息
        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
        final Printer 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);// 分发消息给发送消息的handler处理
        }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 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();
    }
}

相关文章

  • Android线程切换之谜

    之前有写过一篇文章 记一次Handler的优化 ,当时并未详细的讲解Handler的线程切换功能。遂写下这篇文章,...

  • Android 切换线程

    多线程实现3种方式: 1.extends Thread/implements Runnable AsyncTask...

  • 因为我对Handler的了解,居然直接给我加了5K?!

    1 Handler是什么? android提供的线程切换工具类。主要的作用是通过handler实现从子线程切换回主...

  • Android:Handler源码解析

    1、前言 在Android中,Handler消息机制十分常见,在实现多线程消息切换的场景屡屡能看到,比如子线程切换...

  • Android 线程切换,线程池。

    在开发中,我们往往需要用到线程切换这个功能,最常见的使用场景就是,当我们需要做网络请求或者其他耗时处理时,不能在主...

  • Android消息机制Handler

    Handler的作用 Android中从自定义线程切换到UI线程需要用到Handler,向UI线程发送消息需要Ha...

  • Android Handler机制1--Thread

    本篇文章的主要内容如下: 1、Java线程概念2、Android线程的实现3、线程的阻塞4、关于线程上下文切换5、...

  • 7.网络框架

    概念 retrofit、android-async-http、volley,帮你封装了具体的请求,线程切换以及数据...

  • 2019-06-03 面试总结

    Android基础: Handler的机制 线程切换的具体原理?Handler和Looper的对应关系?如何在...

  • 学着造轮子-RxLifeCycle

    使用RxJava的一个很大的优势就是线程的灵活切换,特别是Android开发,工作线程请求,主线程监听,这已经是最...

网友评论

    本文标题:Android线程切换之谜

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