Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android的线程池 #27

Open
soapgu opened this issue Apr 7, 2021 · 0 comments
Open

Android的线程池 #27

soapgu opened this issue Apr 7, 2021 · 0 comments
Labels
Demo Demo 安卓 安卓

Comments

@soapgu
Copy link
Owner

soapgu commented Apr 7, 2021

  • 引言

前面我们安卓着重在于UI线程,主要用Activity.runOnUiThread(Runnable),以及mainThreadHandler方式来把逻辑在UI线程上执行。对于工作线程关心比较少。
如果我们想要把某些逻辑指定在工作线程上执行,避免UI的卡顿等一些因素。我们要怎么做。
new 一个Thread,然后start,简单粗暴好不好??
答案显然是否定的。和其他平台一样Android的线程的的开销还是很大的。

  • 线程池简介

对于工作线程还是用传统的线程池来解决最好。
它平衡了性能、资源使用、资源竞争的关系,几乎所有平台都有线程池的概念

安卓的Executors下有大量的定制好的线程池模型,总有一款适合你!
这里不做大幅介绍
最简单就是单线程线程池
public static ExecutorService newSingleThreadExecutor()
顾名思义,就是永远最多只有一个工作线程,如果同时拥入一堆任务只能排队。资源消耗少,适合一些小耗时,不急用的场景。

  • 线程池应用

原来我们的Execute工具类已经完成了UI线程执行的API包装,那么我们这里扩展一下

public class Execute {
    private static volatile Execute instance;
    private final Handler mainThreadHandler;
    private ExecutorService executor;

    private Execute()
    {
        this.mainThreadHandler = HandlerCompat.createAsync(Looper.getMainLooper());
        this.executor = Executors.newSingleThreadExecutor();
    }

    public static Execute getInstance()
    {
        if( instance == null ) {
            synchronized ( Execute.class ) {
                if( instance == null ){
                    instance = new Execute();
                }
            }
        }
        return instance;
    }

    /**
     * Post Delegate to UI Thread
     * @param r 相关代理
     */
    public void BeginOnUIThread(Runnable r)
    {
        this.mainThreadHandler.post(r);
    }

    /**
     * Excute on sub thread
     * @param r 相关代理
     */
    public void BeginOnSubThread( Runnable r ){
        this.executor.execute( r );
    }
}

接下来是调用部分代码,人脸识别的激活动作在非UI线程执行

Execute.getInstance().BeginOnSubThread( ()->{
                    int result = FaceEngine.activeOffline(getApplicationContext(), DEFAULT_AUTH_FILE_PATH);
                    String notice;
                    switch (result) {
                        case ErrorInfo.MOK:
                            notice = "成功";
                            break;
                        case ErrorInfo.MERR_ASF_ALREADY_ACTIVATED:
                            notice = "已激活";
                            break;
                        case ErrorInfo.MERR_ASF_ACTIVEKEY_ACTIVEKEY_ACTIVATED:
                            notice = "该激活码已被其他设备使用";
                            break;
                        default:
                            notice = "其他错误" + ErrorCodeUtil.arcFaceErrorCodeToFieldName(result);
                            break;
                    }
                    this.runOnUiThread(() -> this.ShowMessage(notice));
                } );
  • 参考资料

Android Develop-通过线程提升性能
简书-Android-Threadpool
简书-Android线程池原理及使用

@soapgu soapgu added Demo Demo 安卓 安卓 labels Apr 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Demo Demo 安卓 安卓
Projects
None yet
Development

No branches or pull requests

1 participant