android开发-线程池

1、android的线程池ThreadPoolExcutor ,他是实现了Excutor接口

2、ThreadPoolExcutor最重要的是他的构造函数

int corePoolSize 核心线程的数目  线程启动之后 无论是否空闲 会一直存在

int maxiumPoolSize 最大线程数目 如果任务比较多 会创建一些临时的线程 临时线程与核心线程 一共的最大的数目

long keepAliveTime 临时线程空闲时间  如果达到空闲时间 那么临时线程就取消掉

TimeUnit unit 时间单位 TimeUnit.SECOND(秒)、 TimeUnit.MILLISECONDS(毫秒)、  TimeUnit.MINUTES(分钟)

BlockingQueue<Runnable> workQueue  如果任务的数目超过了 最大数目 那么任务就要添加到 工作队列进行等待,直到有线程出现空闲!

ThreadFactory threadFactory  为线程池创建新线程的功能 ThreadFactory就是一个接口,只有一个方法 public Thread newThread(new Runnable)

线程池还要设置一个参数RejectExecutionHandler ,来设置 当线程无法执行新任务,而且任务队列也已经满了或者无法成功执行的时候,会执行的策略

executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());  

一般我们有四种策略

AbortPolicy

ThreadPoolExecutor中默认的拒绝策略就是AbortPolicy。直接抛出异常,所以在执行executor.execute(new Runnable)的时候要使用try-catch捕获异常

CallerRunsPolicy

在任务被拒绝添加后,会调用当前线程池的所在的线程去执行被拒绝的任务

DiscardPolicy

这个东西什么都没干,什么都不执行

DiscardOldestPolicy 

当任务呗拒绝添加时,会抛弃任务队列中最旧的任务也就是最先加入队列的,再把这个新任务添加进去。

(来自:http://blog.csdn.net/qq_25806863/article/details/71172823



3、android的AsyncTask就是封装了一个ThreadPoolExcutor

private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
// We want at least 2 threads and at most 4 threads in the core pool,
// preferring to have 1 less than the CPU count to avoid saturating
// the CPU with background work
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);

    public Thread newThread(Runnable r) {
        return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());
    }
};

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

/**
 * An {@link Executor} that can be used to execute tasks in parallel.
 */
public static final Executor 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;
}

int count=Runtime.getRuntime().availableProcessors();//cpu的核心数目

他的核心线程数目是count+1

最大线程数目是:最少2  最多4个

核心线程也有超时机制、线程闲置超时时间是30秒

任务队列的最大容量是128


4、线程池的分类

android中常见的四种线程池,他们都是直接或者间接的通过配置ThreadPoolExecutor来实现自己的功能

FixThreadPool、CacheThreadPool、ScheduledThreadPool、以及SingleThreadExecutor

他们都是通过Executor的静态方法来创建的

(1)FixThreadPool 他是一种线程数量固定的线程池,他只有核心线程,即使线程闲置 也不会回收,新任务都会处于等待状态,直到有线程空闲出来,任务队列没有大小限制

public static ExecutorService newFixedThreadPool(int nThread)

{

return new ThreadPoolExecutor(nThreads,nThreads,0L,TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>())

}

(2)CacheThreadPool 他是线程无量不固定的线程池,他只有非核心线程,没有核心线程,最大的线程数目是Integer.Max_VALUE,相当于线程的数目是无限的,线程中限制的线程有60秒的超时时限,来新任务都会背立刻执行。他的任务队列是SynchronousQueue,一个无法存储元素的集合

public static ExecutorService newCacheThreadPool()

{

return new ThreadPoolExecutor(0,Integer.Max_VALUE,60L,TimeUnit.MILLISECONDS,new SynchronousQueue<Runnable>())

}

(3)ScheduledThradPool 有固定的核心线程数目,非核心线程数目是无限最大的,当非核心线程闲置的时候 会被立刻回收,这类线程池主要是用于执行定时任务和具有固定周期的重复任务

public ScheduleThreadPoolExecutor(int corPoolSize){

super(corePoolSize,Integer.Max_Value, 0 , TimeUnit.NANOSECONDS, new DelayedWorkQueue(0);

}

(4)SingleThreadExecutor 这类线程池内部只有一个核心线程池 没有非核心线程,任务队列没有大下限制,确保所有的任务都在同一个线程中执行,确保了数据同步

public static ExecutorService newSingleThreadExceutor(){

return new FinalizableDelegatedExecutorService( new ThreadPoolExecutor(1,1 ,0L,TimeUnit.MILLIONSECOND, new LinkedBlockingQueue<Runnable>))

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值