ThreadPoolExecutor
ThreadPoolExecutor是线程池的真正实现,它的构造方法提供了一系列参数供外部配置线程池,参数配置不同将会直接影响线程的功能特性,如下是它常用的构造方法。
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, defaultHandler);
corePoolSize: 该线程池中核心线程的数量,默认情况下,核心线程会一直存活在线程池中
maximumPoolSize:线程池中能容纳的最大线程数,当活动线程达到这个数值之后,后续如果再新增任务,都会被加入到阻塞队列中
keepAliveTime:从字面上就可以理解,是非核心线程空闲时要等待下一个任务到来的时间,当任务很多,每个任务执行时间很短的情况下调大该值有助于提高线程利用率。当ThreadPoolExecutor的allowCoreThreadTimeOut属性设为true时,该属性也可用于核心线程。
unit:用于指定keepAliveTime参数的时间单位
workQueue:线程池中的任务队列
threadFactory:线程工厂,可用于设置线程名字等等,一般无须设置 该参数。
ThreadPoolExecutor参数的配置可通过借鉴AsyncTask类的配置进行配置,如下就体现了AsyncTask线程池的配置情况:
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);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor( CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory); threadPoolExecutor.allowCoreThreadTimeOut(true); THREAD_POOL_EXECUTOR = threadPoolExecutor;
线程池的分类
Android中最常见的四类具有不同特性的线程池,他们都是通过间接的配置ThreadPoolExecutor的参数来实现自己独特的功能特性,这个四类线程池分别为,FixedThreadPool、CachedThreadPool、ScheduledThreadPool以及SingleThreadExexutor。
1.FixedThreadPool
它是一种线程数量固定的线程池,当线程处于闲置状态时,不会被回收,除非线程池关闭了。当所有的线程都处于活动状态时,新的任务会处于等待状态,直到有线程空置出来,FixedThreadPool只有核心线程,并且不会被回收,所以FixedThreadPool能够快速响应外界的请求。
2.CachedThreadPool
线程数量不固定,只有核心线程,最大线程数为 Integer.MAX_VALUE,当线程池中的线程都处于活动状态时,线程池会创建新的线程来处理任务,CachedThreadPool有超时机制,超时时间为60秒,超过60秒闲置线程就会被回收,因此当整个线程池都处于闲置状态时,线程池中的所有线程都会因超时被停止,此时CachedThreadPool是不占用任何系统资源的,适合执行大量耗时较少的任务。
3.ScheduledThreadPool
核心线程数量固定,非核心线程数量没有限制,非核心线程闲置时会被立即回收,主要用于执行定时任务和具有固定周期的重复任务。
4.SingleThreadExexutor
只有一个核心线程,所有的任务都在同一个线程中执行,避免了线程同步的问题。
上面对Android常见的4中线程池进行详细的描述,下面将通过代码演示这4中线程池的创建方法,如下
Runnable runnable=new Runnable() { @Override public void run() { SystemClock.sleep(6000); } }; //1.FixedThreadPool ExecutorService fixedThreadPool= Executors.newFixedThreadPool(4); fixedThreadPool.execute(runnable); //2.CachedThreadPool ExecutorService cachedThreadPool=Executors.newFixedThreadPool(); cachedThreadPool.execute(runnable); //2.ScheduledThreadPool ScheduledExecutorService scheduledThreadPoolf=Executors.newScheduledThreadPool(4); scheduledThreadPoolf.schedule(runnable,3000, TimeUnit.MILLISECONDS); scheduledThreadPoolf.scheduleAtFixedRate(runnable,10,2000, TimeUnit.MILLISECONDS); //2.SingleThreadExexutor ExecutorService singleThreadExecutor=Executors.newSingleThreadExecutor(); singleThreadExecutor.execute(runnable);