《Java并发编程的艺术》Executor框架简介(第十章)

工具类Executors可以把一个Runnable对象封装为一个Callable对象:

  1. Executors.callable(Runnable task)

  2. Executors.callable(Runnable task,Object resule)

  3. 然后可以把Runnable对象直接交给ExecutorService执行

ExecutorService.execute(Runnable command)

或者也可以把Runnable对象或Callable对象提交给ExecutorService执行

  • ExecutorService.submit(Runnable task)

  • ExecutorService.submit(Callable<T> task)

  1. 如果执行ExecutorService.submit(…),ExecutorService将返回一个实现Future接口的对象(到目前为止的JDK中,返回的是FutureTask对象)。

由于FutureTask实现了Runnable,程序员也可以创建FutureTask,然后直接交给ExecutorService执行。

  1. 最后:
  • 主线程可以执行FutureTask.get()方法来等待任务执行完成

  • 主线程也可以执行 FutureTask.cancel(boolean mayInterruptIfRunning)来取消此任务的执行

2.2 Executor框架的成员

一、ThreadPoolExecutor

ThreadPoolExecutor通常使用工厂类Executors来创建。Executors可以创建3种类型的ThreadPoolExecutor:SingleThreadExecutorFixedThreadPoolCachedThreadPool

1)FixedThreadPool

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

public static ExecutorService newFixedThreadPool(int nThreads)

public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory)

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

2)SingleThreadExecutor

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

public static ExecutorService newSingleThreadExecutor()

public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory)

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

3)CachedThreadPool

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

public static ExecutorService newCachedThreadPool()

public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory)

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

二、ScheduledThreadPoolExecutor

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

1)ScheduledThreadPoolExecutor

包含若干个线程的ScheduledThreadPoolExecutor。

下面是工厂类Executors提供的,创建固定个数线程的ScheduledThreadPoolExecutor的API:

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory)

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

2)SingleThreadScheduledExecutor

只包含一个线程的ScheduledThreadPoolExecutor。

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

public static ScheduledExecutorService newSingleThreadScheduledExecutor()

public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory)

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

三、Future接口

Future接口和实现Future接口的FutureTask类用来表示异步计算的结果

当我们把Runnable接口或Callable接口的实现类提交(submit)给ThreadPoolExecutor或 ScheduledThreadPoolExecutor时,ThreadPoolExecutor或ScheduledThreadPoolExecutor会向我们返回一个FutureTask对象。

下面是对应的API:

Future submit(Callable task)

Future submit(Runnable task, T result)

Future<> submit(Runnable task)

四、Runnable接口和Callable接口
  • Runnable接口和Callable接口的实现类,都可以被ThreadPoolExecutor或ScheduledThreadPoolExecutor执行。

  • 它们之间的区别是Runnable不会返回结果,而Callable可以返回结果。

面试准备+复习分享:

为了应付面试也刷了很多的面试题与资料,现在就分享给有需要的读者朋友,资料我只截取出来一部分哦

秋招|美团java一面二面HR面面经,分享攒攒人品

e接口和Callable接口的实现类,都可以被ThreadPoolExecutor或ScheduledThreadPoolExecutor执行。

  • 它们之间的区别是Runnable不会返回结果,而Callable可以返回结果。

面试准备+复习分享:

为了应付面试也刷了很多的面试题与资料,现在就分享给有需要的读者朋友,资料我只截取出来一部分哦

[外链图片转存中…(img-ATyQgd5j-1726121112232)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值