android 项目中线程池的使用




public class ThreadPoolUtils
{
    // 线程池核心线程数
    private static int CORE_POOL_SIZE = 5;


    // 线程池最大线程数
    private static int MAX_POOL_SIZE = 100;


    // 额外线程空状态生存时间
    private static int KEEP_ALIVE_TIME = 10000;
    
    /** {@value} */
    public static final int DEFAULT_THREAD_POOL_SIZE = 3;
    /** {@value} */
    public static final int DEFAULT_THREAD_PRIORITY = Thread.NORM_PRIORITY - 1;
    /** {@value} */
    public static final QueueProcessingType DEFAULT_TASK_PROCESSING_TYPE = QueueProcessingType.FIFO;


    // 阻塞队列。当核心线程都被占用,且阻塞队列已满的情况下,才会开启额外线程。
    private static BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(
            20);


    private Handler mHander = new Handler();
    // 线程工厂
    private static ThreadFactory threadFactory = new ThreadFactory()
    {
        private final AtomicInteger integer = new AtomicInteger();


        @Override
        public Thread newThread(Runnable r)
        {
            return new Thread(r, "myThreadPool thread:"
                    + integer.getAndIncrement());
        }
    };
    // 线程池
    private static ExecutorService threadPool;


    static
    {
    /*    threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE,
                KEEP_ALIVE_TIME, TimeUnit.SECONDS, workQueue, threadFactory);*/
        threadPool = createExecutor(DEFAULT_THREAD_POOL_SIZE, DEFAULT_THREAD_PRIORITY , DEFAULT_TASK_PROCESSING_TYPE);
    }
    
    
    
    /**
     * 从线程池中抽取线程,执行指定的Runnable对象
     * 
     * @param runnable
     */
    public static void execute(Runnable runnable, boolean isAwaitTermination, RequestParam param, RequestListener listener)
    {


        try
        {
            threadPool.execute(runnable);
            if (isAwaitTermination)
            {
                threadPool.shutdown();
                try
                {
                    threadPool.awaitTermination(Long.MAX_VALUE,
                            TimeUnit.NANOSECONDS);
                } catch (InterruptedException e)
                {
                }
            }
        } catch (Exception e)
        {
        }
    }
    


    /**
     * 从线程池中抽取线程,执行指定的Runnable对象
     * 
     * @param runnable
     */
    public static void execute(Runnable runnable, boolean isAwaitTermination)
    {


        try
        {
            threadPool.execute(runnable);
            if (isAwaitTermination)
            {
                threadPool.shutdown();
                try
                {
                    threadPool.awaitTermination(Long.MAX_VALUE,
                            TimeUnit.NANOSECONDS);
                } catch (InterruptedException e)
                {
                }
            }
        } catch (Exception e)
        {
        }
    }
    
    /** Creates default implementation of task executor */
    public static ExecutorService createExecutor(int threadPoolSize, int threadPriority, QueueProcessingType tasksProcessingType) {
        boolean lifo = tasksProcessingType == QueueProcessingType.LIFO;
        BlockingQueue<Runnable> taskQueue = lifo ? new LIFOLinkedBlockingDeque<Runnable>() : new LinkedBlockingQueue<Runnable>();
        return new ThreadPoolExecutor(threadPoolSize, threadPoolSize, 0L, TimeUnit.MILLISECONDS, taskQueue, createThreadFactory(threadPriority));
    }
    
    /** Creates default implementation of {@linkplain ThreadFactory thread factory} for task executor */
    private static ThreadFactory createThreadFactory(int threadPriority) {
        return new DefaultThreadFactory(threadPriority);
    }


    private static class DefaultThreadFactory implements ThreadFactory {


        private static final AtomicInteger poolNumber = new AtomicInteger(1);


        private final ThreadGroup group;
        private final AtomicInteger threadNumber = new AtomicInteger(1);
        private final String namePrefix;
        private final int threadPriority;


        DefaultThreadFactory(int threadPriority) {
            this.threadPriority = threadPriority;
            SecurityManager s = System.getSecurityManager();
            group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
            namePrefix = "pool-" + poolNumber.getAndIncrement() + "-thread-";
        }


        public Thread newThread(Runnable r) {
            Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
            if (t.isDaemon()) t.setDaemon(false);
            t.setPriority(threadPriority);
            return t;
        }
    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值