Handler----MessageQueue----Looper的解析

一、Handler使用

(1)handler的初始化

//初始化使用当前线程的looper
public Handler() {
   this(null, false);
}

//使用指定线程的looper  
public Handler(Looper looper) {
   this(looper, null, false);
}

(2)发送消息方式,基本上是两种方式

mHandler.sendEmptyMessage(0);
mHandler.sendEmptyMessageDelayed(0,500);
mHandler.sendMessage(message);
mHandler.sendMessageDelayed(message,500);
mHandler.post(new Runnable() {
     @Override
     public void run() {         
     }
   });
mHandler.postDelayed(new Runnable() {
      @Override
      public void run() {       
      }
     },500);

(3)message的创建

//方式1
Message m = new Message;
//方式2
Message m = Message.obtain;
//方式3
Message m = mHandler.obtainMessage();

  方式二和方式三比较好,因为android默认的消息池中数据是10,这两种方式直接在消息池中取出一个Message实例,这样可以避免创建更多的Message实例。

二、Handler、MessageQueue、Looper三者关系

 1、     初始化Handler,必须先创建Looper;UI线程中我们不需要创建Looper,因为系统在ActivityThread的main()中调用Looper.prepareMainLooper()方法创建了UI线程的Looper,prepareMainLooper方法又调用了prepare方法,prepare方法先从sThreadLocal中取looper,sThreadLocal是ThreadLocal类的实例,如果取出为null,则初始化Looper实例,并通过sThreadLocal的set方法存储起来,方便下载直接get获取到。Looper的初始化时,也初始化了消息队列MessageQueue(单链表),所以Looper获取messageQueue的引用源码如下:

 /**
     * Initialize the current thread as a looper, marking it as an
     * application's main looper. The main looper for your application
     * is created by the Android environment, so you should never need
     * to call this function yourself.  See also: {@link #prepare()}
     */
public static void prepareMainLooper() {
        prepare(false);
        synchronized (Looper.class) {
            if (sMainLooper != null) {
                throw new IllegalStateException("The main Looper has already been prepared.");
            }
            sMainLooper = myLooper();
        }
    }


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));
}


private Looper(boolean quitAllowed) {
        mQueue = new MessageQueue(quitAllowed);
        mThread = Thread.currentThread();
}

2、创建Handler

     在初始化Handler的时候,如果没有指定Looper,那么handler会使用程初始化线程的looper, 通过Looper.myLooper获取当前线程的Looper实例, 如果结果mLooper为空,会抛出异常,所以验证了上面说的,想用Handler就得先创建looper。Handler中也定义了一个变量mQueue,并赋值为当前线程的looper实例的MessageQueue,因此Handler操作Qqueue,就是操作looper实例里面的消息队列源码如下:

 /**
     * Use the {@link Looper} for the current thread with the specified callback interface
     * and set whether the handler should be asynchronous.
     *
     * Handlers are synchronous by default unless this constructor is used to make
     * one that is strictly asynchronous.
     *
     * Asynchronous messages represent interrupts or events that do not require global ordering
     * with respect to synchronous messages.  Asynchronous messages are not subject to
     * the synchronization barriers introduced by {@link MessageQueue#enqueueSyncBarrier(long)}.
     *
     * @param callback The callback interface in which to handle messages, or null.
     * @param async If true, the handler calls {@link Message#setAsynchronous(boolean)} for
     * each {@link Message} that is sent to it or {@link Runnable} that is posted to it.
     *
     * @hide
     */
    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;
}


public static @Nullable Looper myLooper() {
    return sThreadLocal.get();
}

3、handler发送消息

      无论handler调用哪种方式发送消息,最终调用的是Handler的sendMessageDelayed(Message msg,long delayMillis)方法,参数为一个参数为msg,源码如下

/**
     * Enqueue a message into the message queue after all pending messages
     * before (current time + delayMillis). You will receive it in
     * {@link #handleMessage}, in the thread attached to this handler.
     *  
     * @return Returns true if the message was successfully placed in to the 
     *         message queue.  Returns false on failure, usually because the
     *         looper processing the message queue is exiting.  Note that a
     *         result of true does not mean the message will be processed -- if
     *         the looper is quit before the delivery time of the message
     *         occurs then the message will be dropped.
     */
    public final boolean sendMessageDelayed(Message msg, long delayMillis)
    {
        if (delayMillis < 0) {
            delayMillis = 0;
        }
        return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
    }

一般来说,参数msg是我们自己创建的,即使我们没有创建,handler也会自己创建,msg还有个属性target,执行发送这个消息的hardler实例,保证谁发送的消息将来由谁处理。代码如下:

 public final boolean sendEmptyMessageDelayed(int what, long delayMillis) {
        Message msg = Message.obtain();
        msg.what = what;
        return sendMessageDelayed(msg, delayMillis);
    }

       但是,Handler还可以通过post(runnable  r)方式发送消息,所以当使用post方式时Handler调用getPostMessage(r)方法,传入Runnable,返回message,并未message的callback属性赋值为runnable,这里提示一下,处理消息回用到callback属性,源码如下:

  public final boolean postDelayed(Runnable r, long delayMillis)
    {
        return sendMessageDelayed(getPostMessage(r), delayMillis);
    }

 private static Message getPostMessage(Runnable r) {
        Message m = Message.obtain();
        m.callback = r;
        return m;
    }

      封装好参数Message后,调用sendMessageAtTime方法,两个参数一个为msg,一个为时间uptimeMillis。

  public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
        MessageQueue queue = mQueue;
        if (queue == null) {
            RuntimeException e = new RuntimeException(
                    this + " sendMessageAtTime() called with no mQueue");
            Log.w("Looper", e.getMessage(), e);
            return false;
        }
        return enqueueMessage(queue, msg, uptimeMillis);
    }

    首先拿到的是handler中的messageQueue,最后调用的是handler中enqueueMessage方法

  private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
        msg.target = this;
        if (mAsynchronous) {
            msg.setAsynchronous(true);
        }
        return queue.enqueueMessage(msg, uptimeMillis);
    }

       msg.target=this;这段代码为每个msg的target赋值,这用来判断是哪个Handelr实例发送的消息,最终只能由谁处理,最后调用的是MessageQueue的enqueueMessage方法,传入的是参数是msg(msg已经为target赋值了)和时间,里面有个nativewark方法,这是native方法用来唤醒messageQueue的next方法,源码如下:

 boolean enqueueMessage(Message msg, long when) {
        if (msg.target == null) {
            throw new IllegalArgumentException("Message must have a target.");
        }
        if (msg.isInUse()) {
            throw new IllegalStateException(msg + " This message is already in use.");
        }

        synchronized (this) {
            if (mQuitting) {
                IllegalStateException e = new IllegalStateException(
                        msg.target + " sending message to a Handler on a dead thread");
                Log.w(TAG, e.getMessage(), e);
                msg.recycle();
                return false;
            }

            msg.markInUse();
            msg.when = when;
            Message p = mMessages;
            boolean needWake;
            if (p == null || when == 0 || when < p.when) {
                // New head, wake up the event queue if blocked.
                msg.next = p;
                mMessages = msg;
                needWake = mBlocked;
            } else {
                // Inserted within the middle of the queue.  Usually we don't have to wake
                // up the event queue unless there is a barrier at the head of the queue
                // and the message is the earliest asynchronous message in the queue.
                needWake = mBlocked && p.target == null && msg.isAsynchronous();
                Message prev;
                for (;;) {
                    prev = p;
                    p = p.next;
                    if (p == null || when < p.when) {
                        break;
                    }
                    if (needWake && p.isAsynchronous()) {
                        needWake = false;
                    }
                }
                msg.next = p; // invariant: p == prev.next
                prev.next = msg;
            }

            // We can assume mPtr != 0 because mQuitting is false.
            if (needWake) {
                nativeWake(mPtr);
            }
        }
        return true;
    }

4、Looper.loop,开启死循环,不停的去消息队列中取数据,queue.next获取消息,没有消息就阻塞,next方法中有一个nativePollonce方法  没有消息时进入阻塞状态

 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
            final Printer logging = me.mLogging;
            if (logging != null) {
                logging.println(">>>>> Dispatching to " + msg.target + " " +
                        msg.callback + ": " + msg.what);
            }

            final long traceTag = 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 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();
        }
    }

        最后调用的是msg.target.dispatchMessage方法,因为msg.target其实就是Handler的实例,所以具体实现还是由Handler来处理消息

/**
     * Handle system messages here.
     */
    public void dispatchMessage(Message msg) {
        if (msg.callback != null) {
            handleCallback(msg);
        } else {
            if (mCallback != null) {
                if (mCallback.handleMessage(msg)) {
                    return;
                }
            }
            handleMessage(msg);
        }
    }

         首先判断的是msg的callback是否为空,上面描述过,只有通过post方式发送消息,callback才会有值,我们先看callback为null的情况,调用的handlerMessage(msg)方法,这个方法在Handelr是空实现,我们可以覆写,所以最终调用的是我们自己的实现。现在我们看看callback不为空的情况,handleCallback(msg),源码如下:

  private static void handleCallback(Message message) {
        message.callback.run();
    }

message.callback就是Runnable,最后调用的是Runnable的run方法,至此处理消息就完了。

三、Looper死循环为什么不会导致应用卡死

      主线程的主要方法就是消息循环,一旦退出消息循环,那么应用就退出了,Looper.loop()在主线程没有消息可处理的时候是阻塞的,但是只要保证有消息的时候能够立刻处理,消息循环没有被阻塞,就不会产生ANR异常;造成ANR的不是主线程阻塞,而是在looper消息处理过程中发生了任务阻塞,无法及时响应。

四、handler交互

1、方式一

private  Handler mainHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.e("MainActivity","我是在工作线程传过来的消息");
        }
    };
    private Handler threadHandler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        MyThread threadDemo= new MyThread();
        threadDemo.start();

        threadHandler=new Handler(threadDemo.looper){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Log.e("MainActivity","我是在主线程传过来的消息");
                mainHandler.sendEmptyMessage(0);
            }
        };
        threadHandler.sendEmptyMessage(0);
    }

    class  MyThread extends Thread{
        public Looper looper;

        @Override
        public void run() {
            super.run();
            Looper.prepare();
            looper=Looper.myLooper();
            Looper.loop();

        }
    }

2、方式二(推荐)

private  Handler mainHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.e("MainActivity","我是在工作线程传过来的消息");
        }
    };
    private Handler threadHandler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        HandlerThread   handlerThread=new HandlerThread("handler thread");
        handlerThread.start();

        threadHandler=new Handler(handlerThread.getLooper()){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Log.e("MainActivity","我是在主线程传过来的消息");
                mainHandler.sendEmptyMessage(0);
            }
        };
        threadHandler.sendEmptyMessage(0);
    }

五、Handler内存泄漏

(1)原因

        Handler允许我们发送延时消息,在延时期间关闭了activity,那么会导致activity泄漏,因为Message持有handler,而

内部类会持有外部类,所以activity会被handler持有。

(2)解决方案

         将Handler定义成静态内部类,在内部持有Activity的弱引用,并且在Activity的onDestory中取消该handler的所有消息。

static class MyHanlder extends Handler {
    private WeakReference mActivity;//弱引用

    public MyHanlder(HandlerActivity activity) {
        super();
        mActivity = new WeakReference<>(activity);
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        Toast.makeText(mActivity.get(), "发消息了", Toast.LENGTH_SHORT).show();
    }
}


handler.removeCallbacksAndMessages(null);

六、为啥系统不建议在子线程访问UI

       Android的UI控件不是线程安全的,如果在多线程中并发访问会出现不可预期的状态,如果加锁则会使UI访问逻辑复杂,效率降低。

dler.removeCallbacksAndMessages(null);

直通车------ThreadLocal

直通车------HandlerThread

你可能感兴趣的:(Android,Handler,Looper,消息队列,Handler内存泄漏)