Handler,Looper,Message,MessageQueue

首先看下handler的源码

    public interface Callback {
        public boolean handleMessage(Message msg);
    }
    
    /**
     * Subclasses must implement this to receive messages.
     */
    public void handleMessage(Message msg) {
    }
    
    /**
     * Handle system messages here.
MessageQueue里最终调用的就是这个方法,可以看到分3种情况
     */
    public void dispatchMessage(Message msg) {
        if (msg.callback != null) {
            handleCallback(msg);
        } else {
            if (mCallback != null) {
                if (mCallback.handleMessage(msg)) {
                    return;
                }
            }
            handleMessage(msg);
        }
    }

MessageQueue里最终调用的就是Handler的这个dispatchMessage方法,可以看到分3种情况,首先看message有没有设置callback,完事看handler有没有设置callback,并且返回true,最后都没有的话才调用handleMessage方法。
Message是可以设置callback的,下边是初始化方法


Handler,Looper,Message,MessageQueue_第1张图片
image.png

Handler也是可以传个callback的,下图是构造方法


Handler,Looper,Message,MessageQueue_第2张图片
image.png

以前也没仔细看过,

  1. 一直以来都是直接重写handler的handleMessage来处理消息的。
  2. 如下图这种:Message自带一个runnable这种的,到时候收到消息执行的就是run的方法了。
message=Message.obtain(handler, new Runnable() {
            
            @Override
            public void run() {
                
                
            }
        });
  1. 另一种,handler构造方法里可以传一个Callback的参数的,如果这玩意返回true,就表示自己处理了,否则就继续调用Handler里的那个handleMessage方法了。
    public Handler(Callback callback) {
        this(callback, false);
    }
     public interface Callback {
        public boolean handleMessage(Message msg);
    }

Handler内部是有一个Looper的,如果handler是在主线程创建的,那么这个looper不需要处理,系统自己已经初始化了一个looper了。
如果我们在一个新的线程里用handler,那么必须给予一个looper,否则就会挂掉,如下代码:

        new Thread(new Runnable() {
            
            @Override
            public void run() {
            new Handler().post(new Runnable() {
                
                @Override
                public void run() {
                    System.err.println("myLooper=======1========"+Looper.myLooper());
                }
            });
                
            }
        }).start();

运行以后就会挂了,提示如下错误:
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
原因看源码:Handler构造方法里会判断looper有没有初始化,没有它就挂了。

    public Handler(Callback callback, boolean async) {
        if (FIND_POTENTIAL_LEAKS) {
            final Class klass = getClass();
            if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
                    (klass.getModifiers() & Modifier.STATIC) == 0) {
                Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
                    klass.getCanonicalName());
            }
        }

        mLooper = Looper.myLooper();
        if (mLooper == null) {
            throw new RuntimeException(
                "Can't create handler inside thread that has not called Looper.prepare()");
        }
        mQueue = mLooper.mQueue;
        mCallback = callback;
        mAsynchronous = async;
    }

下面是正确的写法:

        new Thread(new Runnable() {

            @Override
            public void run() {
                Looper.prepare();
                
                new Handler().post(new Runnable() {

                    @Override
                    public void run() {
                        System.err.println("myLooper=======1========" + Looper.myLooper());
                                                Looper.myLooper().quit();
                    }
                });

                Looper.loop();//这玩意是个死循环
            }
        }).start();

其中Looper.prepare(); 就是用来给当前线程创建一个Looper
Looper.loop(),用来处理messageQueue里的消息的,所以上边的runnable里的代码需要运行,这两个方法缺一不可。
稍微简单看下Looper的源码:

//构造方法
    private Looper(boolean quitAllowed) {
        mQueue = new MessageQueue(quitAllowed);
        mThread = Thread.currentThread();
    }

//prepare方法就是new一个放到sThreadLocal里
    public static void prepare() {
        prepare(true);
    }

    private static void prepare(boolean quitAllowed) {
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        sThreadLocal.set(new Looper(quitAllowed));
    }

//loop方法就是处理messageQueue里的message的
    /**
     * 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();
        }
    }

继续分析looper这个玩意,大家一直说他是死循环,虽然我也看到了for(;;)可我更看到了if (msg == null)

    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) {//我第一眼看到是这里,我想,我不给你设置message你不就return?might block这个提示我完全没当回事
                // 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);

今天跑去看了下 queue.next();这个方法,原来这个next也是个死循环,我靠,非得有一个message它才返回,所以就解释了为啥上边的msg基本不可能为空,当然了,在looper调用quit方法的时候,也会把MessageQueue里的mQuitting改为真,next里的死循环就会返回null了,上边pool方法里是死循环也就一起结束了。

 Message next() {
        // Return here if the message loop has already quit and been disposed.
        // This can happen if the application tries to restart a looper after quit
        // which is not supported.
        final long ptr = mPtr;
        if (ptr == 0) {
            return null;
        }

        int pendingIdleHandlerCount = -1; // -1 only during first iteration
        int nextPollTimeoutMillis = 0;
        for (;;) {
            if (nextPollTimeoutMillis != 0) {
                Binder.flushPendingCommands();
            }

            nativePollOnce(ptr, nextPollTimeoutMillis);

            synchronized (this) {
                // Try to retrieve the next message.  Return if found.
                final long now = SystemClock.uptimeMillis();
                Message prevMsg = null;
                Message msg = mMessages;
                if (msg != null && msg.target == null) {//不为空
                    // Stalled by a barrier.  Find the next asynchronous message in the queue.
                    do {
                        prevMsg = msg;
                        msg = msg.next;
                    } while (msg != null && !msg.isAsynchronous());
                }
                if (msg != null) {//不为空,为空啥也没干,继续循环
                    if (now < msg.when) {
                        // Next message is not ready.  Set a timeout to wake up when it is ready.
                        nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
                    } else {
                        // Got a message.
                        mBlocked = false;
                        if (prevMsg != null) {
                            prevMsg.next = msg.next;
                        } else {
                            mMessages = msg.next;
                        }
                        msg.next = null;
                        if (DEBUG) Log.v(TAG, "Returning message: " + msg);
                        msg.markInUse();
                        return msg;
                    }
                } else {
                    // No more messages.
                    nextPollTimeoutMillis = -1;
                }

                // Process the quit message now that all pending messages have been handled.
                if (mQuitting) {//looper调用quit方法,也会调用messageQueue里的quit方法,把这个值改成true的。
                    dispose(); 
                    return null;
                }

最后总结一下:

//如下是一个新开的线程里的方法片段
Looper.prepare();
                
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        
                         Looper looper=Looper.myLooper();
                        if(looper!=null) {
                            looper.quit();
                        }
                    }
                },3333);

                Looper.loop();

我们可以看到3步走

1 Looper.prepare(); 初始化Looper

2 Handler post 一个Runnable,其实就是往MessageQueue里添加一个新的Message

3 开始loop循环从MessageQueue里取Message

其实还有个第4步,就是调用quit方法退出。如果不调用quit方法,Looper.loop();后边再写代码就不会执行了。loop是个死循环。

实际中的应用

如果要new一个非主线程的looper,我们一般是如下做法

        val handlerThread=HandlerThread("aa")
        handlerThread.start()
        val handler=Handler(handlerThread.looper)
//        handler.sendEmptyMessage(1)
        handlerThread.quit()

handlerthread 调用start的时候会初始化一个looper的。完事quit的时候会调用looper的quit
看下HandlerThread的run方法

    public void run() {
        mTid = Process.myTid();
        Looper.prepare();
        synchronized (this) {
            mLooper = Looper.myLooper();
            notifyAll();
        }
        Process.setThreadPriority(mPriority);
        onLooperPrepared();
        Looper.loop();
        mTid = -1;
    }

quit方法

    public boolean quit() {
        Looper looper = getLooper();
        if (looper != null) {
            looper.quit();
            return true;
        }
        return false;
    }

关系总结

首先Handler需要一个Looper【Looper里包含一个Messagequeue】
Handler和Message之间最后都是通过messagequeue.enqueueMessage(msg, uptimeMillis)把message放到messagequeue里。
完事looper在执行loop方法以后会有个循环获取messagequeue里的消息,通过next()方法,而
messagequeue.next()方法是个block的,有数据就返回,没数据就阻塞。所以looper的loop方法也就一直在阻塞的,要退出需要调用quit方法

其他一些东西

我们的activity是有一个ActivityThread的东西的。
百度搜索 :Android 主线程looper
看这里:http://blog.csdn.net/u013435893/article/details/50903082
为撒死循环不会挂掉:https://www.zhihu.com/question/34652589

打印下一般的activity启动了几个线程

int count = Thread.activeCount();
                Thread[] threads=new Thread[count];
                Thread.enumerate(threads);
                for(Thread thread:threads) {
                    System.err.println("==="+thread.toString());
                }
03-15 04:25:31.061: W/System.err(16637): ===Thread[Binder_4,5,main]
03-15 04:25:31.061: W/System.err(16637): ===Thread[Binder_3,5,main]
03-15 04:25:31.061: W/System.err(16637): ===Thread[RenderThread,5,main]
03-15 04:25:31.061: W/System.err(16637): ===Thread[Binder_2,5,main]
03-15 04:25:31.061: W/System.err(16637): ===Thread[Binder_1,5,main]
03-15 04:25:31.061: W/System.err(16637): ===Thread[main,5,main]

可能是经常new一个Thread出来里边都带一个Runnable的构造方法,所以啊,今天执行下面的代码

        new Runnable() {
            
            @Override
            public void run() {
                System.err.println("Runnable=======2========" + Looper.myLooper());
            }
        }.run();

我还想着是个新线程,其实还是主线程。哎,thread和runnable天天黏在一起,害的我把他俩当一回事了。

你可能感兴趣的:(Handler,Looper,Message,MessageQueue)