HandlerThread的一些理解

来源:互联网 发布:h5 banner轮播js源码 编辑:程序博客网 时间:2024/05/17 02:24

HandlerThread本质是一个线程,是为了方便自定义与线程相关的handler,这样handler接收处理消息就不会在main线程当中,可以在handler中处理耗时的操作。

这个是HandlerThread中的getLooper方法

public Looper getLooper() {    if (!isAlive()) {        return null;    }        // If the thread has been started, wait until the looper has been created.    synchronized (this) {        while (isAlive() && mLooper == null) {            try {                wait();            } catch (InterruptedException e) {            }        }    }    return mLooper;}
这个方法配合
HandlerThread handler = new HandlerThread("handler");Handler handler1 = new Handler(handler.getLooper()){    @Override    public void handleMessage(Message msg) {        super.handleMessage(msg);    }};

这样就可以创造出在线程中处理消息的handler

0 0