Android线程池封装实践2019-12-05

2019-12-05  本文已影响0人  勇往直前888

封装需求

new Thread(new Runnable).start();用起来很方便,不过据说容易导致内存溢出的问题,不能常用。
ThreadPoolExecutor能很好地管理内存,防止内存溢出,不过参数稍微多了点,不好用,所以封装一下。
自定义一个类,比如ThreadUtil之类的,将一些繁琐的参数封装一下,方便使用。

自定义线程池

就是参数自己给,有什么需要自己加,这里是最简单的,只是给个名字。

    // cpu核心数
    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
    // 线程池核心线程数
    private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
    // 线程池核心线程数
    private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
    // 线程超时时长
    private static final long KEEP_ALIVE = 10;
    // 线程工厂
    private static final ThreadFactory threadFactory = new ThreadFactory() {
        private final AtomicInteger count = new AtomicInteger(1);

        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "ThreadUtil#" + count.getAndIncrement());
        }
    };
    // 线程池
    private static final Executor customerExecutor = new ThreadPoolExecutor(
            CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS,
            new LinkedBlockingDeque<Runnable>(), threadFactory);

    /**
     * 自定义线程池
     * @param runnable runnable
     */
    public static void customer(Runnable runnable) {
        customerExecutor(runnable);
    }

使用的时候,只要ThreadUtil. customer(Runnable);就可以了。

固定线程池

    // 固定线程池的线程数量
    private static final int FIXED_THREAD_NUMBER = 5;
    private static final ExecutorService fixedExecutor = Executors.newFixedThreadPool(FIXED_THREAD_NUMBER);

    /**
     *  固定线程池 运行
     * @param runnable runnable
     */
    public static void fixed(Runnable runnable) {
        fixedExecutor.execute(runnable);
    }

单线程池

    // 单线程池
    private static final ExecutorService singleExecutor = Executors.newSingleThreadExecutor();

    /**
     * 单线程池 运行
     * @param runnable runnable
     */
    public static void single(Runnable runnable) {
        singleExecutor.execute(runnable);
    }

缓存线程池

    // 缓存线程池
    private static final ExecutorService cachedExecutor = Executors.newCachedThreadPool();

    /**
     * 缓存线程池 运行
     * @param runnable runnable
     */
    public static void cached(Runnable runnable) {
        cachedExecutor.execute(runnable);
    }

定时线程池

    // 定时线程池
    private static final ScheduledExecutorService scheduleExecutor = Executors.newScheduledThreadPool(CORE_POOL_SIZE);

    /**
     *  延时执行
     * @param runnable runnable
     * @param seconds  延时的秒数
     */
    public static void after(Runnable runnable, int seconds) {
        scheduleExecutor.schedule(runnable, seconds, TimeUnit.SECONDS);
    }

    /**
     * 延时并周期执行
     * @param runnable runnable
     * @param after    延时的秒数
     * @param cycle    周期间隔的秒数
     */
    public static void afterAndCycle(Runnable runnable, int after, int cycle) {
        scheduleExecutor.scheduleAtFixedRate(runnable, after, cycle, TimeUnit.SECONDS);
    }

参考文章

Android开发——Android中常见的4种线程池(保证你能看懂并理解)
android中的线程池 ThreadPoolExecutor
使用ThreadFactory

上一篇下一篇

猜你喜欢

热点阅读