android线程与线程池-----线程池(二)《android开发艺术与探索》

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));  
    }  

 

posted @ 2016-07-04 14:01  small_role  阅读(564)  评论(0编辑  收藏  举报