美文网首页
Android线程池<备忘录>

Android线程池<备忘录>

作者: SimpleGk | 来源:发表于2018-11-11 21:58 被阅读0次

    public ThreadPoolExecutor(int corePoolSize,

    int maximumPoolSize,

    long keepAliveTime,

    TimeUnit unit,

    BlockingQueue<Runnable> workQueue,

    ThreadFactory threadFactory)


    corePoolSize

    核心线程数目,默认,核心线程会一直存活,除非ThreadPoolExecutor的allowCoreThreadTimeOut属性为true。

    maximumPoolSize

    线程池能容纳的最大线程数量

    keepAliveTime

    非核心线程的闲置时长,当allowCoreThreadTimeOut属性为true,对核心线程也有效。

    unit

    指定keepAliveTime的时间单位,枚举,毫秒TimeUnit.MILLISECONDS,秒TimeUnit.Second,分TimeUnit.MINUTES。

    workQueue

    任务队列,通过execute提交的Runnable储存在这

    threadFactory

    为线程池提供创建线程的功能。

    FixedThreadPool

    ExecutorService pool= Executors.newFixedThreadPool();


    线程数目固定,只有核心线程数(不会被回收),当所以线程处于活动时,新任务会处于等待状态。能快速反应外界请求

    CachedThreadPool

    ExecutorService pool= Executors.newCachedThreadPool();


    只有非核心线程数目,超时60s,线程数目不固定,适合执行大量消耗时间少的任务。

    ScheduledThreadPool

    1.ExecutorService pool= Executors.newScheduledThreadPool(3);

    2.ScheduledExecutorService pool=Executor.newScheduledThreadPool(3);

    //延迟2秒后执行command

    pool.schedule(command,2000,TimeUnit.MILLISECONDS);

    //延迟1秒后每2秒执行一次command

    pool.scheduleAtFixedRate(command,1000,2000,TimeUnit.MILLISECONDS);


    核心线程数目固定。非核心线程数目不定,闲置立即被回收,适合执行定时或者固定周期任务。

    ExecutorService

    ExecutorService pool= Executors.newSingleThreadExecutor();


    只有一个核心线程,所有任务排队执行。

    相关文章

      网友评论

          本文标题:Android线程池<备忘录>

          本文链接:https://www.haomeiwen.com/subject/fweffqtx.html