框架【库】安卓中级进阶

新闻类App (MVP + RxJava + Retrofit+

2019-04-22  本文已影响131人  Peakmain

Github地址:新闻类App (MVP + RxJava + Retrofit+Dagger+ARouter)

线程调度原理

线程调度模型

Android线程调度

nice值

cgroup

1.线程过多会导致CPU切换频繁,降低线程运行效率
2.正确认识任务重要性j决定哪种优先级
3.优先级具有继承性

android异步方式汇总

Thread

HandlerThread

IntentService

AsyncTask

线程池

Rxjava

Android线程优化实战

异步使用规则

实战

public class ThreadPoolUtils {
    /**
     * 会提示:手动创建线程池,效果会更好哦
     */
    private static ExecutorService sService = Executors.newFixedThreadPool(5, new ThreadFactory() {
        @Override
        public Thread newThread(@NonNull Runnable runnable) {
            Thread thread = new Thread();
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            thread.setName("ThreadPoolUtils's ExecutorService");
            return thread;
        }
    });

    public static ExecutorService getService() {
        return sService;
    }
}

使用

     ThreadPoolUtils.getService().execute(new Runnable() {
            @Override
            public void run() {
                //提高优先级
                Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
                String oldName = Thread.currentThread().getName();
                //方便查看哪个线程报错
                Thread.currentThread().setName("MainActivity Thread Name");
                Thread.currentThread().setName(oldName);
            }
        });
    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
    private static final int CORE_POOL_SIZE = Math.max(2, Math.min(CPU_COUNT - 1, 4));
    private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
    private static final int KEEP_ALIVE_SECONDS = 30;

    private static final ThreadFactory sThreadFactory = new ThreadFactory() {
        private final AtomicInteger mCount = new AtomicInteger(1);

        @Override
        public Thread newThread(Runnable r) {
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            return new Thread(r, "ThreadPoolUtils's ThreadFactory" + mCount.getAndIncrement());
        }
    };

    private static final BlockingQueue<Runnable> sPoolWorkQueue =
            new LinkedBlockingQueue<Runnable>(128);
    private static final ThreadPoolExecutor THREAD_POOL_EXECUTOR;


    static {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,
                sPoolWorkQueue, sThreadFactory);
        threadPoolExecutor.allowCoreThreadTimeOut(true);
        THREAD_POOL_EXECUTOR = threadPoolExecutor;
    }
    public static ThreadPoolExecutor getThreadPoolExecutor() {
        return THREAD_POOL_EXECUTOR;
    }

锁定线程创建者

锁定线程创建背景

方案分析

优雅的方案:ARTHook

        DexposedBridge.hookAllConstructors(Thread.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                Thread thread = (Thread) param.thisObject;
                LogUtils.i(thread.getName() + " stack " + Log.getStackTraceString(new Throwable()));
            }
        });

线程收敛方案

    private static ExecutorService sExecutorService;

    public static void setExecutorService(ExecutorService executorService) {
        sExecutorService = executorService;
    }

    public static void exectur(Runnable runnable) {
        //如果外部库不为空,则使用外部,否则使用自己的
        if (sExecutorService != null) {
            sExecutorService.execute(runnable);
        } else {
            ThreadPoolUtils.getService().execute(runnable);
        }
    }

注意:1.要区分任务类型:IO还是CPU密集型,IO就是上面优化实战的第一种方式Executors,CPU就是第二种方式手动创建
2.IO密集型任务不消耗CPU,核心池可以很大
3.CPU密集型任务:核心池大小h和CPU核心数有关

上一篇 下一篇

猜你喜欢

热点阅读