聊聊高并发(四十四)解析java.util.concurrent各个组件(二十) Executors工厂类

Executor框架为了更方便使用,提供了Executors这个工厂类。通过一系列的静态工厂方法。能够高速地创建对应的Executor实例。


仅仅有一个nThreads參数的newFixedThreadPool方法会创建一个ThreadPoolExecutor,corePoolSize和maximumPoolSize都是nThreads。而且keepAliveTime为0表示不会设置过期时间,採用LinkedBlockingQueue作为工作队列

这种方法创建的ThreadPoolExecutor採用固定线程数nThreads,当线程少于nThreads时会为新的任务创建新的Worker工作线程,直到线程数达到nThreads。线程达到nThreads后不会回收。兴许新建的任务会进入工作队列,工作队列是无界的。当任务量过大时。可能会由于无界的工作队列造成OOM的问题。


 public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }

这种方法和上面的方法基本一致。仅仅是多了一个ThreadFactory。能够自己定义创建的线程属性。

 public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>(),
                                      threadFactory);
    }

这个newSingleThreadExecutor是上面的方法基本一致,仅仅是创建了单线程的线程池

 public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }

这种方法和上面的方法基本一致,仅仅是多了一个ThreadFactory。能够自己定义创建的线程属性。


public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>(),
                                    threadFactory));
    }


这个newCachedThreadPool返回一个ThreadPoolExecutor,corePoolSize为0。maximumPoolSize为Integer.MAX_VALUE,表示的意思是线程数没有限制。

KeepAliveTime为60秒,表示的意思是当线程空暇时间超过60秒才会回收线程。

这个就是所谓的Cache。空暇的意思之前说了。表示Worker在工作队列中取任务时,假设超过60秒没取到任务,这个线程就超时,要被回收。採用了SynchronousQueue同步队列作为工作队列。意思是来一个新任务就把任务交给Worker工作线程,不入队列。假设没有可用的工作线程,就创建新的工作线程。这种方法的问题是当任务量大时。会消耗太多的CPU资源,创建太多线程。增大线程上线文切换等消耗。

 public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }

这种方法和上面的方法基本一致,仅仅是多了一个ThreadFactory。能够自己定义创建的线程属性。

 public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>(),
                                      threadFactory);
    }

这种方法创建单线程的ScheduledThreadPoolExecutor。DelegatedScheduleExecutorService是个包装类。将ScheduledThreadPoolExecutor的对外接口缩小

 public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
        return new DelegatedScheduledExecutorService
            (new ScheduledThreadPoolExecutor(1));
    }


这种方法和上面的方法基本一致,仅仅是多了一个ThreadFactory,能够自己定义创建的线程属性。

public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
        return new DelegatedScheduledExecutorService
            (new ScheduledThreadPoolExecutor(1, threadFactory));
    }

这种方法创建corePoolSize个线程的ScheduledThreadPoolExecutor。其它特性和newFixedThreadPool(nThreads)一致

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }
这种方法和上面的方法基本一致,仅仅是多了一个ThreadFactory,能够自己定义创建的线程属性。

public static ScheduledExecutorService newScheduledThreadPool(
            int corePoolSize, ThreadFactory threadFactory) {
        return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
    }

这种方法把Runnable接口适配成Callable接口

 public static <T> Callable<T> callable(Runnable task, T result) {
        if (task == null)
            throw new NullPointerException();
        return new RunnableAdapter<T>(task, result);
    }



原文地址:https://www.cnblogs.com/llguanli/p/7048069.html