线程池原理(六):Executors

来源:互联网 发布:淘宝3c认证可以用其他 编辑:程序博客网 时间:2024/06/07 07:17

Executors是线程池的工厂类,可以创建ExecutorServiceScheduledExecutorServiceThreadFactoryCallable的实例。

根据阿里java编码规范,是【强制】不允许通过Executors创建线程池的,而是通过ThreadPoolExecutor的构造函数创建线程池,这样的处理方式让开发同学更加明确线程池的运行规则,规避资源耗尽的风险,规范中也说明了Executors的问题所在:

  • newFixedThreadPoolnewSingleThreadExecutor

    堆积的请求处理队列可能会耗费非常大的内存,甚至OOM。

  • newCachedThreadPoolnewScheduledThreadPool

    创建的最大线程数是Integer.MAX_VALUE,可能会创建数量非常多的线程,甚至OOM。

虽然如此,从技术的角度来看,有必要学习下Executors

newFixedThreadPool

public static ExecutorService newFixedThreadPool(int nThreads) {        return new ThreadPoolExecutor(nThreads, nThreads,                                      0L, TimeUnit.MILLISECONDS,                                      new LinkedBlockingQueue<Runnable>());}//指定了线程工厂public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {        return new ThreadPoolExecutor(nThreads, nThreads,                                      0L, TimeUnit.MILLISECONDS,                                      new LinkedBlockingQueue<Runnable>(),                                      threadFactory);}

该静态方法创建corePoolSizemaximumPoolSize相等的ThreadPoolExecutor,核心线程不会超时退出,等待队列指定使用LinkedBlockingQueue。第二个重载的方法指定了线程工厂。

newSingleThreadExecutor

public static ExecutorService newSingleThreadExecutor() {        return new FinalizableDelegatedExecutorService            (new ThreadPoolExecutor(1, 1,                                    0L, TimeUnit.MILLISECONDS,                                    new LinkedBlockingQueue<Runnable>()));}//指定了线程工厂public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {        return new FinalizableDelegatedExecutorService            (new ThreadPoolExecutor(1, 1,                                    0L, TimeUnit.MILLISECONDS,                                    new LinkedBlockingQueue<Runnable>(),                                    threadFactory));}

该静态方法创建corePoolSizemaximumPoolsize都等于1的ThreadPoolExecutor,核心线程不会退出,等待队列指定使用LinkedBlockingQueue。第二个重载的方法指定了线程工厂。

注意,这个静态方法返回的是一个FinalizableDelegatedExecutorService,该类继承了DelegatedExecutorService。首先看下DelegatedExecutorService源码:

static class DelegatedExecutorService extends AbstractExecutorService {    //具体实现委托给了传入的ExecutorService    private final ExecutorService e;    DelegatedExecutorService(ExecutorService executor) { e = executor; }    public void execute(Runnable command) { e.execute(command); }    public void shutdown() { e.shutdown(); }    public List<Runnable> shutdownNow() { return e.shutdownNow(); }    public boolean isShutdown() { return e.isShutdown(); }    public boolean isTerminated() { return e.isTerminated(); }    public boolean awaitTermination(long timeout, TimeUnit unit)      throws InterruptedException {      return e.awaitTermination(timeout, unit);    }    public Future<?> submit(Runnable task) {      return e.submit(task);    }    public <T> Future<T> submit(Callable<T> task) {      return e.submit(task);    }    public <T> Future<T> submit(Runnable task, T result) {      return e.submit(task, result);    }    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)      throws InterruptedException {      return e.invokeAll(tasks);    }    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,                                         long timeout, TimeUnit unit)      throws InterruptedException {      return e.invokeAll(tasks, timeout, unit);    }    public <T> T invokeAny(Collection<? extends Callable<T>> tasks)      throws InterruptedException, ExecutionException {      return e.invokeAny(tasks);    }    public <T> T invokeAny(Collection<? extends Callable<T>> tasks,                           long timeout, TimeUnit unit)      throws InterruptedException, ExecutionException, TimeoutException {      return e.invokeAny(tasks, timeout, unit);    }}

显然,DelegatedExecutorService利用了代理设计模式思想,它继承了AbstractExecutorService,只暴露出了ExecutorService方法。因为ThreadPoolExecutor还有setter方法用来设置线程池的大小,而我们要创建single ThreadExecutor,显然不能够修改线程池大小了。DelegatedExecutorService代理模式将真正的实现委托给了ThreadPoolExecutor

再来看下FinalizableDelegatedExecutorService,该类的实现非常简单:

static class FinalizableDelegatedExecutorService        extends DelegatedExecutorService {    FinalizableDelegatedExecutorService(ExecutorService executor) {      super(executor);    }    //在GC时增加了关闭线程池的操作    protected void finalize() {      super.shutdown();    }}

继承了DeletatedExecutorService,在GC时增加了关闭线程池的操作。

newCachedThreadExecutor

public static ExecutorService newCachedThreadPool() {        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,                                      60L, TimeUnit.SECONDS,                                      new SynchronousQueue<Runnable>());}//指定了线程工厂public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,                                      60L, TimeUnit.SECONDS,                                      new SynchronousQueue<Runnable>(),                                      threadFactory);}

该静态方法创建corePoolSize=0maximumPoolSize=Integer.MAX_VALUEThreadPoolExecutor,线程超时时间为60s,等待队列指定使用SynchronousQueue。第二个重载的方法指定了线程工厂。

newSingleThreadScheduledExecutor

public static ScheduledExecutorService newSingleThreadScheduledExecutor() {        return new DelegatedScheduledExecutorService            (new ScheduledThreadPoolExecutor(1));}//指定了线程工厂public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {        return new DelegatedScheduledExecutorService            (new ScheduledThreadPoolExecutor(1, threadFactory));}

该静态方法创建corePoolSize=1、maximumPoolSize=Integer.MAX_VALUEScheduledThreadPoolExecutor,这里的Single指的是核心线程数等于1。第二个重载的方法指定了线程工厂。

该方法返回DelegatedScheduledExecutorService,显然该类也是一个代理类。ScheduledThreadPoolExecutor可以通过setter方法设置线程池的大小,而我们要创建的是Single线程的。该代理类包装了ScheduledExecutorService,只暴露出ScheduledExecutorService的方法,将具体实现委托给了ScheduledThreadPoolExecutor。看下类DelegatedScheduledExecutorService的实现:

static class DelegatedScheduledExecutorService            extends DelegatedExecutorService            implements ScheduledExecutorService {        //实现委托给了ScheduledExecutorService的一个实例        private final ScheduledExecutorService e;        DelegatedScheduledExecutorService(ScheduledExecutorService executor) {            super(executor);            e = executor;        }        public ScheduledFuture<?> schedule(Runnable command, long delay,  TimeUnit unit) {            return e.schedule(command, delay, unit);        }        public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {            return e.schedule(callable, delay, unit);        }        public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay,  long period, TimeUnit unit) {            return e.scheduleAtFixedRate(command, initialDelay, period, unit);        }        public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay,  long delay, TimeUnit unit) {            return e.scheduleWithFixedDelay(command, initialDelay, delay, unit);        }}

newScheduledThreadPool

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {        return new ScheduledThreadPoolExecutor(corePoolSize);}//指定了线程工厂public static ScheduledExecutorService newScheduledThreadPool(            int corePoolSize, ThreadFactory threadFactory) {        return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);}

该静态方法创建核心线程数为corePoolSize,最大线程数为maximumPoolSizeScheduledThreadPoolExecutor

unconfigurableExecutorService

顾名思义,该方法创建不可配制的ExecutorService,不可配制表示该线程池不能够通过setter方法设置线程池的参数。

public static ExecutorService unconfigurableExecutorService(ExecutorService executor) {    if (executor == null)      throw new NullPointerException();    return new DelegatedExecutorService(executor);}

返回DelegatedExecutorService的实例。

unconfigurableScheduledExecutorService

该方法创建不可配制的ScheduledExecutorService。

public static ScheduledExecutorService unconfigurableScheduledExecutorService(ScheduledExecutorService executor) {    if (executor == null)      throw new NullPointerException();    return new DelegatedScheduledExecutorService(executor);}

返回DelegatedScheduledExecutorService的实例。

defaultTheadFactory

该方法返回默认的线程工厂,DefaultThreadFactory是线程工厂的默认实现。

public static ThreadFactory defaultThreadFactory() {    return new DefaultThreadFactory();}//默认线程工厂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;    DefaultThreadFactory() {      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);      if (t.getPriority() != Thread.NORM_PRIORITY)        t.setPriority(Thread.NORM_PRIORITY);      return t;    }}

默认线程工厂实现了ThreadFactory接口,创建线程的时候指定了线程组、线程名字。其中线程组和线程名字通过poolNumberthreadNumber原子变量进行编号。

callable

该方法将Runnable包装成一个Callable,通过返回RunnableAdapter实例实现

public static <T> Callable<T> callable(Runnable task, T result) {    if (task == null)      throw new NullPointerException();    return new RunnableAdapter<T>(task, result);}public static Callable<Object> callable(Runnable task) {    if (task == null)      throw new NullPointerException();    return new RunnableAdapter<Object>(task, null);}//Runnable的适配器类,适配成Callablestatic final class RunnableAdapter<T> implements Callable<T> {    final Runnable task;    final T result;    RunnableAdapter(Runnable task, T result) {      this.task = task;      this.result = result;    }    public T call() {      task.run();      return result;    }}
原创粉丝点击