Executor框架

Executor框架

1.简介

Java并发库提供一种灵活的线程池实现作为Executor框架的一部分,在Java类库中,任务执行的主要抽象不是Thread而是Executor

public interface Executor {

    /**
     * Executes the given command at some time in the future.  The command
     * may execute in a new thread, in a pooled thread, or in the calling
     * thread, at the discretion of the {@code Executor} implementation.
     *
     * @param command the runnable task
     * @throws RejectedExecutionException if this task cannot be
     * accepted for execution
     * @throws NullPointerException if command is null
     */
    void execute(Runnable command);
}

Executor虽然是个简单的借口但是它却为灵活且强大的异步任务执行框架提供了基础,该框架能支持多种不同类型的任务执行策略,Executor的实现还提供了对生命周期的支持。以及统计信息收集、应用程序管理机制和性能监控等机制。
Executor基于生产者-消费者模式,提交任务相当于生产者,执行任务的线程相当于消费者。

2.Executor实现

  • SingleThreadExecutor使用单线程执行任务,Executors提供的API有如下两个:
public static ExecutorService newSingleThreadExecutor();
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory);

SingleThreadExecutor保证了任务执行的顺序,不会存在多线程活动。

  • FixedThreadPool是使用固定线程数的线程池,Executors提供的API有如下两个
public static ExecutorService newFixedThreadPool(int nThreads);
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory);

FixedThreadPool满足了资源管理的需求,可以限制当前线程数量。适用于负载较重的服务器环境。

  • CachedThreadPool是无界线程池,Executors提供的API有如下两个
public static ExecutorService newCachedThreadPool();
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory);

CachedThreadPool适用于执行很多短期异步任务的小程序,适用于负载较轻的服务器。

  • ScheduledThreadPoolExecutor
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize);
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory);

ScheduledThreadPoolExecutor具有固定线程个数,适用于需要多个后台线程执行周期任务,并且为了满足资源管理需求而限制后台线程数量的场景,Executors中提供的API有如下两个:

  • SingleThreadScheduledExecutor具有单个线程,Executors提供的创建API有如下两个:
public static ScheduledExecutorService newSingleThreadScheduledExecutor();
public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory);

它适用于单个后台线程执行周期任务,并且保证顺序一致执行的场景。

上述的ThreadPoolExecutor和ScheduledThreadPoolExecutor都可以用于执行Runnable与Callable接口的实现类

3.Executor周期

Executor的实现通常会创建线程来执行任务,但JVM只有在所有(非守护)线程全部终止后才会退出,因此如果无法正确的关闭Executor,那么JVM将无法结束。
为了解决执行服务的生命周期问题,Executor扩展了ExecutorService接口,添加了一些用于生命周期管理的方法。

public interface ExecutorService extends Executor {   
void shutdown();   
List<Runnable> shutdownNow();   
boolean isShutdown();   
boolean isTerminated();   
boolean awaitTermination(long timeout, TimeUnit unit)   
        throws InterruptedException;   
} 
  • shutdown方法:这个方法会平滑地关闭ExecutorService,当我们调用这个方法时,ExecutorService停止接受任何新的任务且等待已经提交的任务执行完成(已经提交的任务会分两类:一类是已经在执行的,另一类是还没有开始执行的),当所有已经提交的任务执行完毕后将会关闭ExecutorService。这里我们先不举例在下面举例。

  • awaitTermination方法:这个方法有两个参数,一个是timeout即超时时间,另一个是unit即时间单位。这个方法会使线程等待timeout时长,当超过timeout时间后,会监测ExecutorService是否已经关闭,若关闭则返回true,否则返回false。一般情况下会和shutdown方法组合使用

    扫描二维码关注公众号,回复: 1439847 查看本文章
  • shutdownNow方法:这个方法会强制关闭ExecutorService,它将取消所有运行中的任务和在工作队列中等待的任务,这个方法返回一个List列表,列表中返回的是等待在工作队列中的任务
  • isTerminated方法:这个方法会校验ExecutorService当前的状态是否为“TERMINATED”即关闭状态,当为“TERMINATED”时返回true否则返回false
  • isShutdown方法:这个方法在ExecutorService关闭后返回true,否则返回false

猜你喜欢

转载自blog.csdn.net/rambokitty/article/details/80492712