10-1节
Executer 的两级调度模型
hotspot vm 的线程模型中, java 线程(Thread)被一一映射为一个操作系统内核线程。当java线程结束时候。对应的操作系统内核线程将被回收。操作系统调度所有的内核线程。并且将它们分配给可用的cpu
在上层,java多线程将应用分配成多个任务,并且有用户级别 调度器(Executer)将任务映射成多个线程,在底层,操作系统将这些线程,应射到硬件处理器上。Executer 控制上层的任务调度,底层的调度有操作系统调度。下层的调度不受应用程序的控制。这就是两级调度模型。
Executor框架的结构与成员

Executer框架
Executor框架主要由3大部分组成如下。
- 任务。包括被执行任务需要实现的接口:Runnable接口或Callable接口。
- 任务的执行。包括任务执行机制的核心接口Executor,以及继承自Executor的ExecutorService接口。Executor框架有两个关键类实现了ExecutorService接口(ThreadPoolExecutor和ScheduledThreadPoolExecutor)。
- 异步计算的结果。包括接口Future和实现Future接口的FutureTask类。

下面是这些类和接口的简介。
- Executor是一个接口,它是Executor框架的基础,它将任务的提交与任务的执行分离开来。
- ThreadPoolExecutor是线程池的核心实现类,用来执行被提交的任务。
- ScheduledThreadPoolExecutor是一个实现类,可以在给定的延迟后运行命令,或者定期执行命令。ScheduledThreadPoolExecutor比Timer更灵活,功能更强大。
- Future接口和实现Future接口的FutureTask类,代表异步计算的结果。
- Runnable接口和Callable接口的实现类,都可以被ThreadPoolExecutor或ScheduledThreadPoolExecutor执行。

2.Executor框架的成员
绍Executor框架的主要成员:ThreadPoolExecutor、ScheduledThreadPoolExecutor、Future接口、Runnable接口、Callable接口和Executors。
一、ThreadPoolExecutor通常使用工厂类Executors来创建。
Executors可以创建3种类型ThreadPoolExecutor:SingleThreadExecutor、FixedThreadPool和CachedThreadPool。
- FixedThreadPool。下面是Executors提供的,创建使用固定线程数的FixedThreadPool的API。
FixedThreadPool适用于为了满足资源管理的需求,而需要限制当前线程数量的应用场景,它适用于负载比较重的服务器。
public static ExecutorService newFixedThreadPool(int nThreads)public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactorythreadFactory factory)
SingleThreadExecutor。下面是Executors提供的,创建使用单个线程的SingleThreadExecutor的API SingleThreadExecutor适用于需要保证顺序地执行各个任务;并且在任意时间点,不会有多个线程是活动的应用场景。
public static ExecutorService newSingleThreadExecutor() public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory)CachedThreadPool。下面是Executors提供的,创建一个会根据需要创建新线程的CachedThreadPool的API
CachedThreadPool是大小无界的线程池,适用于执行很多的短期异步任务的小程序,或者是负载较轻的服务器。
public static ExecutorService newCachedThreadPool()
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory)
二、ScheduledThreadPoolExecutor
ScheduledThreadPoolExecutor通常使用工厂类Executors来创建。Executors可以创建2种类型的ScheduledThreadPoolExecutor,如下
1. ScheduledThreadPoolExecutor
工厂类Executors提供的,创建固定个数线程的ScheduledThreadPoolExecutor的API
特点:ScheduledThreadPoolExecutor适用于需要多个后台线程执行周期任务,同时为了满足资源
管理的需求而需要限制后台线程的数量的应用场景
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
public static ScheduledExecutorService newScheduledThreadPool(ThreadFactory threadFactory)
2. SingleThreadScheduledExecutor
Executors提供的,创建单个线程的SingleThreadScheduledExecutor的API
特点:SingleThreadScheduledExecutor适用于需要单个后台线程执行周期任务,同时需要保证顺序地执行各个任务的应用场景。
public static ScheduledExecutorService newSingleThreadScheduledExecutor()
public static ScheduledExecutorService newSingleThreadScheduledExecutor
(ThreadFactory threadFactory)
三、Future 接口
Future接口和实现Future接口的FutureTask类用来表示异步计算的结果。当我们把Runnable接口或Callable接口的实现类提交(submit)给ThreadPoolExecutor或ScheduledThreadPoolExecutor时,ThreadPoolExecutor或ScheduledThreadPoolExecutor会向我们返回一个FutureTask对象。下面是对应的API。
<T> Future<T> submit(Callable<T> task)
<T> Future<T> submit(Runnable task, T result)
Future<> submit(Runnable task)
注意: 到目前最新的JDK 8为止,Java通过上述API返回的是一个FutureTask对象。但从API可以看到,Java仅仅保证返回的是一个实现了Future接口的对象。在将来的JDK实现中,返回的可能不一定是FutureTask。
——————————————————————————-以上是第十章第一节内容———————————————————
