標籤:
android 中的線程池
線程池的優點:
1 重用線程池中的線程,避免了線程的建立和銷毀帶來的效能開銷
2 能有效控制最大並發數,避免大量線程之間因為喜歡搶資源而導致阻塞
3 能夠對線程進行簡單的管理,提供定時執行以及指定間隔時間迴圈執行等
android 中的線程池源自java 中的Executor,Executor是一個介面,正真的實現是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 非核心線程閑置時的逾時時間長度,超過這個時間長度,非核心線程就會被回收
unit 用於指定keepAliveTime參數的時間單位。
workQueue 線程池中的任務隊列
threadFactory 線程工廠
ThreadPoolExecutor執行任務時大致遵循的規則:
1 如果線程池中的線程數量未達到核心線程的數量,那麼會直接啟動一個核心線程來執行任務。
2 如果線程池中的線程數量已達到或者超過核心線程的數量,那麼任務會被插入到任務隊列中排隊等待執行
3 如果2中無法將任務插入到任務隊列,這往往是由於任務隊列已滿,這個時候如果線程數量未達到線程池規定的最大值,那麼會立刻啟動一個非核心線程執行任務
4 如果3中線程數量達到線程池規定的最大值,那麼就拒絕執行此任務ThreadPoolExecutor會調用rejectedExecution的rejectedExecution 來通知調用者
這個是AsyncTask的線程池的配置:
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors(); private static final int CORE_POOL_SIZE = CPU_COUNT + 1;//核心線程數等於CPU核心數+1 private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;//線程池的最大線程數為CPU核心數的2倍+1 private static final int KEEP_ALIVE = 1;//核心線程無逾時機制,非核心線程逾時1秒 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()); } }; //任務任務隊列容量128 private static final BlockingQueue<Runnable> sPoolWorkQueue = new LinkedBlockingQueue<Runnable>(128); public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);
下面來看線程池的分類
Executors類建立線程池:
FixedThreadPool Executors類newFixedThreadPool建立: public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); } public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory); }
CachedThreadPool public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); } public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), threadFactory); }
ScheduledThreadPool public static ScheduledExecutorService newSingleThreadScheduledExecutor() { return new DelegatedScheduledExecutorService (new ScheduledThreadPoolExecutor(1)); } public static ScheduledExecutorService newScheduledThreadPool( int corePoolSize, ThreadFactory threadFactory) { return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory); }
SingleThreadExecutor public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); } public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) { return new DelegatedScheduledExecutorService (new ScheduledThreadPoolExecutor(1, threadFactory)); }
android線程與線程池-----線程池(二)《android開發藝術與探索》