Android线程池简介和基本应用

一、为什么使用线程池?

1.线程的创建和销毁由线程池维护,节约系统的开销;
2.执行大量异步任务时,线程池可以提高性能;
3.控制线程最大并发数,线程的定时任务和单线程的执行任务。

二、Android线程池框架结构主要包括3个部分:

1.任务:需要实现的接口类:Runnable或Callable
2.任务的执行器:核心接口类Executor
3.执行器的创建者:工厂类Executors(包括ThreadPoolExecutor,ScheduledThreadPoolExecutor)

三、线程池使用方法和参数解析

1、初始化一个任务执行器。

private static final int CORE_POOL_SIZE = 4;
private static final int MAX_POOL_SIZE = 8;
private static final int KEEP_ALIVE_TIME = 10;
private final Executor mExecutor;
mExecutor = new ThreadPoolExecutor(
       CORE_POOL_SIZE,MAX_POOL_SIZE,KEEP_ALIVE_TIME,
        TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(),
        new PriorityThreadFactory("thread-pool", Process.THREAD_PRIORITY_BACKGROUND));

参数解析:
CORE_POOL_SIZE:核心线程,即使它们处在空闲状态,也要保留在池中的线程数
MAX_POOL_SIZE:池中允许的最大线程数
KEEP_ALIVE_TIME:当线程数大于核心线程时,多余线程在终止之前等待新任务的最长时间
TimeUnit.SECONDS:keepAliveTime的时间单位,这里为秒
new LinkedBlockingQueue():workQueue,在执行任务之前用于保存任务的队列,这里的LinkedBlockingQueue是一种有界的阻塞队列
new PriorityThreadFactory(“thread-pool”, Process.THREAD_PRIORITY_BACKGROUND):工厂类,执行程序创建的新线程使用的工厂(该参数可不设置)

2.创建一个简单任务

private class Worker implements Runnable{
    @Override
    public void run(){
        //do something
    }
}

3.执行任务。

Worker w = new Worker();
mExecutor.execute(w);

四、线程池类封装和应用

使用线程池的好处之一就是在需要大量使用线程的场合,或者当线程里发生了阻塞或者耗时时,由线程池来管理线程的创建和销毁,避免出现大量线程没有销毁的问题。

线程池的类:

/**
 * Offloads work from other threads by running it in a background thread.
 */
public class BackgroundExecutor {

    private static final BackgroundExecutor sInstance = new BackgroundExecutor();

//查看newFixedThreadPool实际上是使用上述new ThreadPoolExecutor创建
    private final ExecutorService mExecutorService = Executors.newFixedThreadPool(2);

    /**
     * @return the static instance of the background executor.
     */
    public static BackgroundExecutor get() {
        return sInstance;
    }

    /**
     * Runs the given {@param callable} on one of the background executor threads.
     */
    public <T> Future<T> submit(Callable<T> callable) {
        return mExecutorService.submit(callable);
    }

    /**
     * Runs the given {@param runnable} on one of the background executor threads.
     */
    public Future<?> submit(Runnable runnable) {
        return mExecutorService.submit(runnable);
    }

    /**
     * Runs the given {@param runnable} on one of the background executor threads. Return
     * {@param result} when the future is resolved.
     */
    public <T> Future<T> submit(Runnable runnable, T result) {
        return mExecutorService.submit(runnable, result);
    }
}

应用:

private final BackgroundExecutor mBackgroundExecutor;

mBackgroundExecutor = BackgroundExecutor.get();

mBackgroundExecutor.submit(new Runnable() {
    @Override
    public void run() {
		//do something
    }
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunxiaolin2016

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值