Android线程池<备忘录>

public ThreadPoolExecutor(int corePoolSize,

int maximumPoolSize,

long keepAliveTime,

TimeUnit unit,

BlockingQueue<Runnable> workQueue,

ThreadFactory threadFactory)


corePoolSize

核心线程数目,默认,核心线程会一直存活,除非ThreadPoolExecutor的allowCoreThreadTimeOut属性为true。

maximumPoolSize

线程池能容纳的最大线程数量

keepAliveTime

非核心线程的闲置时长,当allowCoreThreadTimeOut属性为true,对核心线程也有效。

unit

指定keepAliveTime的时间单位,枚举,毫秒TimeUnit.MILLISECONDS,秒TimeUnit.Second,分TimeUnit.MINUTES。

workQueue

任务队列,通过execute提交的Runnable储存在这

threadFactory

为线程池提供创建线程的功能。


FixedThreadPool

ExecutorService pool= Executors.newFixedThreadPool();


线程数目固定,只有核心线程数(不会被回收),当所以线程处于活动时,新任务会处于等待状态。能快速反应外界请求

CachedThreadPool

ExecutorService pool= Executors.newCachedThreadPool();


只有非核心线程数目,超时60s,线程数目不固定,适合执行大量消耗时间少的任务。

ScheduledThreadPool

1.ExecutorService pool= Executors.newScheduledThreadPool(3);

2.ScheduledExecutorService pool=Executor.newScheduledThreadPool(3);

//延迟2秒后执行command

pool.schedule(command,2000,TimeUnit.MILLISECONDS);

//延迟1秒后每2秒执行一次command

pool.scheduleAtFixedRate(command,1000,2000,TimeUnit.MILLISECONDS);



核心线程数目固定。非核心线程数目不定,闲置立即被回收,适合执行定时或者固定周期任务。

ExecutorService

ExecutorService pool= Executors.newSingleThreadExecutor();


只有一个核心线程,所有任务排队执行。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容