Executors工厂方法说明

newFixedTreadPool:返回一个固定线程数量的线程池,该方法创建的线程池线程数量始终不变,此线程池使用的任务队列为LinkedBlockingQueue。(任务队列描述说明链接:ThreadPoolExecutor主要参数说明_誰嘚的博客-CSDN博客

newSingleThreadExecutor:返回一个只有一个线程的线程池,此线程池使用的任务队列为LinkedBlockingQueue。

newCachedThreadPool:返回一个可根据实际情况调整线程数量的线程池,线程池线程数量不确定,但若有空闲的线程可以复用,则会优先使用可复用的线程。若所有线程均在工作,则会创建线程处理任务,所有线程在当前任务执行完毕后,将返回线程池进行复用。此线程池使用的任务队列为SynchronousQueue。

newScheduledThreadPool:返回一个ScheduledExecutorService对象,但该线程池可以指定线程数量。

------------------

        ScheduledExecutorService接口主要方法介绍:

                /*

                *给定时间对任务进行一次调度 

                */

        public ScheduledFuture<?> schedule(Runnable command,
                                   long delay, TimeUnit unit);

                /*

                *给定时间对任务进行周期性调度,以开始时间为起点,之后的period时间,调度下一次任务

                */

        public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                              long initialDelay,
                                              long period,
                                              TimeUnit unit);

                /*

                *给定时间对任务进行周期性调度,以任务结束时间为起点,再经过delay时间,调度下一次任务 

                */

        public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                 long initialDelay,
                                                 long delay,
                                                 TimeUnit unit);

        -------------------

无论是newFixedThreadPool、newSingleThreadExecutor还是newCachedThreadPool都是使用ThreadPoolExecutor实现的,具体如下:

public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>(),
                                  threadFactory);
}
public static ExecutorService newSingleThreadExecutor() {
    return new FinalizableDelegatedExecutorService
        (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>()));
}
public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>());
}

​​​​​​​

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值