Android小知识-什么是HandlerThread

作者: 爱读书的顾先生 | 来源:发表于2018-10-04 14:39 被阅读12次
    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;
        }
    
        public HandlerThread(String name, int priority) {
            super(name);
            mPriority = priority;
        }
    
    
        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;
        }
    
        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;
        }
    
        @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;
        }
    
        public int getThreadId() {
            return mTid;
        }
    }
    

    一言不合贴代码还请海涵,幸好代码不多。

    什么是HandlerThread,简单粗暴来讲就是Handler+Thread+Looper。

    频繁的创建和销毁线程是很耗资源的,因此推荐使用HandlerThread。

    从上面代码中可以看出HandlerThread继承自Thread,那么HandlerThread就是一个线程类,并且内部有自己的Looper对象,可以进行looper循环,我们通过获取HandlerThread的Looper对象并将Looper对象传递给Handler对象,可以在HandleMessage方法中执行异步任务。

    HandlerThread的优点是执行任务时不会堵塞,减少了对性能的消耗,缺点是不能同时进行多个任务的执行,因为是串行队列,需要等待上一个任务执行完成才能进行下一个任务的处理,处理效率较低。

    内部run方法:

    public class HandlerThread extends Thread {
    
        @Override
        public void run() {
            mTid = Process.myTid();
            Looper.prepare();
            synchronized (this) {
                mLooper = Looper.myLooper();
                notifyAll();
            }
            Process.setThreadPriority(mPriority);
            onLooperPrepared();
            Looper.loop();
            mTid = -1;
        }
    
        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;
        }
    
    
    }
    

    在run方法中创建当前线程的Looper对象,并开启消息队列的循环,synchronized同步代码块保证多个线程同时执行这段代码时,只有一个线程执行,当Looper创建完毕后会调用notifyAll方法通知下面的getLooper方法不再处于阻塞状态。

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

    quit和quitSafely方法都是使looper退出当前线程,quitSafely方法更安全但效率上没有quit高。

    扫码_搜索联合传播样式-标准色版.png

    Android、Java、Python、Go、PHP、IOS、C++、HTML等等技术文章,更有各种书籍推荐和程序员资讯,快来加入我们吧!关注技术共享笔记。

    838794-506ddad529df4cd4.webp.jpg

    定期推送优质文章

    相关文章

      网友评论

        本文标题:Android小知识-什么是HandlerThread

        本文链接:https://www.haomeiwen.com/subject/jgyxaftx.html