Executor框架结构与主要成员(一)

北慕城南 提交于 2019-12-11 16:46:11

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

本文分两部分来介绍Executor:Executor的结构和Executor框架包含的成员组件

1、Executor框架的结构

    Executor主要由3大部分组成。

    1.1、任务。包含被执行任务需要实现的接口:Runnable接口或Callable接口。

    1.2、任务的执行。包括任务执行机制的核心接口Executor,以及继承自Executor接口的ExecutorService接口。Executor有两个关键类实现了ExecutorService接口(ThreadPoolExecutor和ScheduledThreadPoolExecutor)

    1.3、异步计算的结果。包括接口Future和实现Future接口的FutureTask类。

    下面是这些类和接口的简介。

    Executor是一个接口,它是Executor框架的基础,它将任务的提交和任务的执行分离开来。

    ThreadPoolExecutor是线程池的核心实现类,用来执行被提交的任务。

    ScheduledThreadPoolExecutor是一个实现类,可以在给定的延迟后运行命令,或者定期执行命令。SchduledThreadPoolExecutor比Timer更灵活,功能更强大。

    Future接口和实现Future接口的FutureTask类,代表异步计算的结果

    Runnable接口和Callable接口的实现类,都可以被ThreadPoolExecutor或SchduledThreadPoolExecutor执行


2、Executor框架的成员

介绍Executor框架的主要成员:ThreadPoolExecutor、ScheduledThreadPoolExecutor、Future接口、Runnable接口、Callable接口、Executors。

2.1、ThreadPoolExecutor

ThreadPoolExecutor通常使用工厂类Executors来创建,Executors可以创建3种类型的ThreadPoolExecutor:SingleThreadExecutor、FixThreadPool、CachedThreadPool。

下面分别介绍这3种ThreadPoolExecutor。

1)FixedThreadPool。下面是Executors提供的,创建使用固定线程数的FixedThreadPool的API

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);
}

FixedThreadPool适用于为了满足资源管理的需求,而需要限制当前线程数量的应用场景,它适用于负载比较重的服务器。

2)SingleThreadExecutor。下面是Executors提供的,创建使用单个线程的SingleThreadExecutor的API

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));
}

SingleThreadExecutor适用于需要保证顺序执行各个任务;并且在任意时间点,不会有多个线程是活动着的应用场景。

3)CachedThreadPool。下面是Executors提供的,创建一个会根据需要而创建新线程的CachedThreadPool的API

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);
}

CachedThreadPool是大小无界的线程池,适用于执行很多短期异步任务的小程序,或者是负载比较轻的服务器。

2.2、SchduledThreadPoolExecutor

SchduledThreadPoolExecutor通常使用工厂类Executors来创建,Executors可以创建2中类型的SchduledThreadPoolExecutor,如下:

SchduledThreadPoolExecutor。包含若干个线程的SchduledThreadPoolExecutor。

SingleThreadSchduledExecutor。只包含一个线程的SchduledThreadPoolExecutor。

1)SchduledThreadPoolExecutor。下面是Executors提供的,创建固定个数的线程SchduledThreadPoolExecutor的API。

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

SchduledThreadPoolExecutor适用于需要多个后台线程执行的周期任务,同时为了满足资源管理的需求而需要限制后台线程数量的场景。

2)SingleThreadSchduledExecutor。下面是Executors提供的,创建单个线程的SingleThreadSchduledExecutor的API。

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

SingleThreadSchduledExecutor适用于需要单个后台线程执行周期任务,同时需要保证顺序地执行各个任务的场景。

3、Future接口

Future接口和实现Future接口的FutureTask类用来表示异步计算的结果,当我们把Runnable接口或者Callable接口的实现类提交(submit)给ThreadPoolExecutor或者SchduledThreadPoolExecutor时,ThreadPoolExecutor或者SchduledThreadPoolExecutor会向我们返回一个FutureTask对象。下面是对应的API

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);
}

4、Runnable和Callable接口

Runnable和Callable接口的实现类都可以被hreadPoolExecutor或者SchduledThreadPoolExecutor执行。它们之间的区别是Runnable不会返回结果,而Callable可以返回结果。

除了可以自已创建实现Callable接口的对象外,还可以使用工厂类Executors来把一个Runnable包装成一个Callable。

下面是Executors提供的,把一个Runnable包装成Callable的API

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

下面是Executors提供的,把一个Runnable和一个待返回的结果包装成一个Callable的API

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

当我们把一个Callable对象提交给ThreadPoolExecutor或者SchduledThreadPoolExecutor执行时,summit()会向我们返回一个FutureTask对象。我们可以执行FutureTask.get()来等待任务执行完成。当任务完成后FutureTask.get()将会返回任务的结果。

注明:好记性不如烂笔头,以上内容来自《Java并发编程的艺术》


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!