前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android HandlerThread分析[通俗易懂]

Android HandlerThread分析[通俗易懂]

作者头像
全栈程序员站长
发布2022-07-31 16:48:35
4400
发布2022-07-31 16:48:35
举报

大家好,又见面了,我是你们的朋友全栈君。

之前Handler 分析的文章有分析过,子线程Thread中是不能直接使用Handler的,需要调用Looper.prepare()方法,因此Android就为我们提供了Handler和Thread结合的方法HandlerThread方法,我们先来看下HandlerThread的源码:

代码语言:javascript
复制
public class HandlerThread extends Thread {
    int mPriority;
    int mTid = -1;
    Looper mLooper;
    private @Nullable Handler mHandler;

    public HandlerThread(String name) {
        super(name);
        mPriority = Process.THREAD_PRIORITY_DEFAULT;
    }

    /**
     * Constructs a HandlerThread.
     * @param name
     * @param priority The priority to run the thread at. The value supplied must be from
     * {@link android.os.Process} and not from java.lang.Thread.
     */
    public HandlerThread(String name, int priority) {
        super(name);
        mPriority = priority;
    }

    /**
     * Call back method that can be explicitly overridden if needed to execute some
     * setup before Looper loops.
     */
    protected void onLooperPrepared() {
    }

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

    /**
     * This method returns the Looper associated with this thread. If this thread not been started
     * or for any reason isAlive() returns false, this method will return null. If this thread
     * has been started, this method will block until the looper has been initialized.
     * @return The looper.
     */
    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;
    }

    /**
     * @return a shared {@link Handler} associated with this thread
     * @hide
     */
    @NonNull
    public Handler getThreadHandler() {
        if (mHandler == null) {
            mHandler = new Handler(getLooper());
        }
        return mHandler;
    }
    public boolean quit() {
        Looper looper = getLooper();
        if (looper != null) {
            looper.quit();
            return true;
        }
        return false;
    }
    public boolean quitSafely() {
        Looper looper = getLooper();
        if (looper != null) {
            looper.quitSafely();
            return true;
        }
        return false;
    }

    /**
     * Returns the identifier of this thread. See Process.myTid().
     */
    public int getThreadId() {
        return mTid;
    }
}

很简单的源码HandlerThread继承Thread主要看:

代码语言:javascript
复制
@Override
    public void run() {
        mTid = Process.myTid();
        Looper.prepare();//手动为子线程创建了looper
        synchronized (this) {
            mLooper = Looper.myLooper();
            notifyAll();
        }
        Process.setThreadPriority(mPriority);
        onLooperPrepared();
        Looper.loop();//开始轮询
        mTid = -1;
    }

可以看到run方法中为子线程创建了looper,并把对象放到线程中,然后通过Looper.loop();开启循环消息。

其他方法:

getThreadHandler: 获取当前线程的handler

quit:清空所有消息

quitSafely:只情况延时消息

getLooper:获取线程中的looper对象

我们看下HandlerThread的使用:

代码语言:javascript
复制
private void startThread(){
    HandlerThread handlerThread = new HandlerThread("thread_one");
    handlerThread.start();
    workHandler = new Handler(handlerThread.getLooper()){//子线程handler
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                Thread.sleep(1000);//子线程中处理耗时操作
                mainHandler.sendEmptyMessage(0);//处理完后发送消息给主线程更新UI

            }catch (Exception e){

            }
        }
    };

    workHandler.sendEmptyMessage(0);

    mainHandler = new Handler(){//主线程handler
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mBtn.setText("start");//主线程中更新UI
        }
    };
}

例子中我们看到我们通过handlerThread.getLooper()获取子线程的looper然后与workHandler,我们知道handler的线程是依赖与与之关联的looper线程,所以workHandler是子线程的handler.

然后我们new 一个未传looper的handler,默认与主线程关联,所以mainHandler是主线程的handler.

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/127757.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档