工具类Executors
可以把一个Runnable对象封装为一个Callable对象:
-
Executors.callable(Runnable task)
-
Executors.callable(Runnable task,Object resule)
-
然后可以把Runnable对象直接交给ExecutorService执行:
ExecutorService.execute(Runnable command)
或者也可以把Runnable对象或Callable对象提交给ExecutorService执行:
-
ExecutorService.submit(Runnable task)
-
ExecutorService.submit(Callable<T> task)
- 如果执行ExecutorService.submit(…),ExecutorService将返回一个实现Future接口的对象(到目前为止的JDK中,返回的是
FutureTask
对象)。
由于FutureTask实现了Runnable,程序员也可以创建FutureTask,然后直接交给ExecutorService执行。
- 最后:
-
主线程可以执行
FutureTask.get()
方法来等待任务执行完成 -
主线程也可以执行
FutureTask.cancel(boolean mayInterruptIfRunning)
来取消此任务的执行
2.2 Executor框架的成员
一、ThreadPoolExecutor
ThreadPoolExecutor通常使用工厂类Executors来创建。Executors可以创建3种类型的ThreadPoolExecutor:SingleThreadExecutor
、FixedThreadPool
和CachedThreadPool
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可以返回结果。
面试准备+复习分享:
为了应付面试也刷了很多的面试题与资料,现在就分享给有需要的读者朋友,资料我只截取出来一部分哦
e接口和Callable接口的实现类,都可以被ThreadPoolExecutor或ScheduledThreadPoolExecutor执行。
- 它们之间的区别是Runnable不会返回结果,而Callable可以返回结果。
面试准备+复习分享:
为了应付面试也刷了很多的面试题与资料,现在就分享给有需要的读者朋友,资料我只截取出来一部分哦
[外链图片转存中…(img-ATyQgd5j-1726121112232)]