浅显易懂handler原理

首先展示一个主线程向子线程发消息的案例。通过这个案例来了解下线程

//创建子线程
     class MyThread extends Thread{
         private Looper looper;//取出该子线程的Looper
         public void run() {

             Looper.prepare();//创建该子线程的Looper
             looper = Looper.myLooper();//取出该子线程的Looper
             Looper.loop();//只要调用了该方法才能不断循环取出消息
         }
     }

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        myThread = new MyThread("handler thread");
        myThread.start();
        mHandler = new Handler(myThread.getLooper()) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Log.d("SecondActivity", "handleMessage: " + Thread.currentThread());
            }
        };
        mHandler.sendEmptyMessage(1);
    }

  1. 在消息接收线程MyThread中创建调用 Looper.prepare() 创建looper。
  2. Looper.myLooper() 保存当前的looper对象
  3. Looper.loop() 不断轮询MessegeQueue,获取到msg。然后调用msg.target.dispatchMessage(msg);msg.target实际上就是handler。dispatchMessage会调用handleMessage(msg)。这样handler就收到消息了。
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();
    }

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;
        //...
        for (;;) {
            Message msg = queue.next(); // might block
            if (msg == null) {
                // No message indicates that the message queue is quitting.
                return;
            }
          //..
            try {
                msg.target.dispatchMessage(msg);
               ...
            } finally {
             ...
            }
           ...
    }

  1. 消息接收线程创建handler,并且获取MyThead线程的looper对象及looper中的messgeQueue对象
 public Handler(Looper looper, Callback callback, boolean async) {
        mLooper = looper;
        mQueue = looper.mQueue;
        mCallback = callback;
        mAsynchronous = async;
    }
  1. 通过调用sendEmptyMessage向messageQueue中添加msg。这里源码就不粘贴了,就是msg插入到链表Message mMessages中

这样就完成了通信,总的来说就是一方通过looper轮询,另一方拿到looper向looper的messageQueue中发送消息。
但是要注意的一点,以上代码实际运行会报looper的messageQueue为空,解决方法就是使用HandlerThread。因为在主线程发消息时,子线程looper可能还没创建好。继承HandlerThread就好了

class MyThread extends HandlerThread {

        public MyThread(String name) {
            super(name);
        }

        @Override
        public void run() {
            super.run();
        }
    }

最后放张关系流程图片


handler.png

你可能感兴趣的:(浅显易懂handler原理)