Android开发线程池管理类之ThreadPoolExecutor工具类

平常开发中网络请求自从Android4.0后强制要求放到子线程中避免主线程进行网络请求,一般很多人习惯直接通过new Thread的方法。不建议这样使用。我们用ThreadPoolExecutor线程池这个类创建比较好。所以有了下面的线程池工具类。

先来张JDK1.6的中文参数说明

Android开发线程池管理类之ThreadPoolExecutor工具类_第1张图片

package com.xiayiye.honorfirst.utils;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author xiayiye
 * 线程池工具
 */
public class ThreadUtils {
    /**
     * 根据cup核心数设置线程池数量
     */
    private static int corePoolSize = Runtime.getRuntime().availableProcessors();
    /**
     * 最大线程池数量= cpu核心数*2+1
     */
    private static int maximumPoolSize = corePoolSize * 2 + 1;
    /**
     * 等待线程的存活时间
     */
    private static long keepAliveTime = 1;
    /**
     * 等待线程存活时间的单位
     */
    private static TimeUnit unit = TimeUnit.HOURS;
    /**
     *
     */
    private static BlockingQueue workQueue = new LinkedBlockingQueue<>();
    /**
     * 默认的线程工厂
     */
    private static ThreadFactory threadFactory = Executors.defaultThreadFactory();
    /**
     * 取消策略,当超过等待线程池的数量后禁止添加了
     */
    private static RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
    private static ThreadPoolExecutor executor;

    /**
     * 开启线程池
     *
     * @param runnable runnable
     */
    public static void startThread(Runnable runnable) {
        executor = new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime, unit,
                workQueue,
                threadFactory,
                handler);
        executor.execute(runnable);
    }

    /**
     * 取消线程池
     *
     * @param runnable runnable
     */
    public static void cancelThread(Runnable runnable) {
        if (null == runnable || executor == null) {
            return;
        }
        executor.remove(runnable);
    }
}

 

调用就更简单了。

ThreadUtils.startThread(runnable);
ThreadUtils.cancelThread(runnable);

一个是开启线程池,一个是取消线程池

你可能感兴趣的:(Android面试)