public class ThreadPool extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
static int |
CachedThread |
static int |
FixedThread |
static int |
SingleThread |
| 构造器和说明 |
|---|
ThreadPool(int type,
int corePoolSize)
ThreadPool构造函数
|
| 限定符和类型 | 方法和说明 |
|---|---|
boolean |
awaitTermination(long timeout,
TimeUnit unit)
请求关闭、发生超时或者当前线程中断 无论哪一个首先发生之后,都将导致阻塞,直到所有任务完成执行。
|
void |
execute(List<Runnable> commands)
在未来某个时间执行给定的命令链表 该命令可能在新的线程、已入池的线程或者正调用的线程中执行,这由 Executor 实现决定。
|
void |
execute(Runnable command)
在未来某个时间执行给定的命令 该命令可能在新的线程、已入池的线程或者正调用的线程中执行,这由 Executor 实现决定。
|
int |
getCorePoolSize() |
<T> List<Future<T>> |
invokeAll(Collection<? extends Callable<T>> tasks)
执行给定的任务 当所有任务完成时,返回保持任务状态和结果的Future列表。
|
<T> List<Future<T>> |
invokeAll(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
执行给定的任务 当所有任务完成或超时期满时(无论哪个首先发生),返回保持任务状态和结果的Future列表。
|
<T> T |
invokeAny(Collection<? extends Callable<T>> tasks)
执行给定的任务 如果某个任务已成功完成(也就是未抛出异常),则返回其结果。
|
<T> T |
invokeAny(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
执行给定的任务 如果在给定的超时期满前某个任务已成功完成(也就是未抛出异常),则返回其结果。
|
boolean |
isShutDown()
判断线程池是否已关闭
|
boolean |
isTerminated()
关闭线程池后判断所有任务是否都已完成 注意,除非首先调用 shutdown 或 shutdownNow,否则 isTerminated 永不为 true。
|
<V> ScheduledFuture<V> |
schedule(Callable<V> callable,
long delay,
TimeUnit unit)
延迟执行Callable命令
|
ScheduledFuture<?> |
schedule(Runnable command,
long delay,
TimeUnit unit)
延迟执行Runnable命令
|
ScheduledFuture<?> |
scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit)
延迟并以固定休息时间循环执行命令
|
ScheduledFuture<?> |
scheduleWithFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit)
延迟并循环执行命令
|
void |
shutdown()
待以前提交的任务执行完毕后关闭线程池 启动一次顺序关闭,执行以前提交的任务,但不接受新任务。
|
List<Runnable> |
shutdownNow()
试图停止所有正在执行的活动任务 试图停止所有正在执行的活动任务,暂停处理正在等待的任务,并返回等待执行的任务列表。
|
<T> Future<T> |
submit(Callable<T> task)
提交一个Callable任务用于执行 如果想立即阻塞任务的等待,则可以使用
result = exec.submit(aCallable).get();
形式的构造。 |
Future<?> |
submit(Runnable task)
提交一个Runnable任务用于执行
|
<T> Future<T> |
submit(Runnable task,
T result)
提交一个Runnable任务用于执行
|
public static final int FixedThread
public static final int CachedThread
public static final int SingleThread
public ThreadPool(int type,
int corePoolSize)
type - 线程池类型corePoolSize - 只对Fixed和Scheduled线程池起效public int getCorePoolSize()
public void execute(Runnable command)
该命令可能在新的线程、已入池的线程或者正调用的线程中执行,这由 Executor 实现决定。
command - 命令public void execute(List<Runnable> commands)
该命令可能在新的线程、已入池的线程或者正调用的线程中执行,这由 Executor 实现决定。
commands - 命令链表public void shutdown()
启动一次顺序关闭,执行以前提交的任务,但不接受新任务。 如果已经关闭,则调用没有作用。
public List<Runnable> shutdownNow()
试图停止所有正在执行的活动任务,暂停处理正在等待的任务,并返回等待执行的任务列表。
无法保证能够停止正在处理的活动执行任务,但是会尽力尝试。
public boolean isShutDown()
true: 是false: 否public boolean isTerminated()
注意,除非首先调用 shutdown 或 shutdownNow,否则 isTerminated 永不为 true。
true: 是false: 否public boolean awaitTermination(long timeout,
TimeUnit unit)
throws InterruptedException
无论哪一个首先发生之后,都将导致阻塞,直到所有任务完成执行。
timeout - 最长等待时间unit - 时间单位true: 请求成功false: 请求超时InterruptedException - 终端异常public <T> Future<T> submit(Callable<T> task)
如果想立即阻塞任务的等待,则可以使用result = exec.submit(aCallable).get();
形式的构造。
T - 泛型task - 任务get方法在成功完成时将会返回该任务的结果。public <T> Future<T> submit(Runnable task, T result)
T - 泛型task - 任务result - 返回的结果get方法在成功完成时将会返回该任务的结果。public Future<?> submit(Runnable task)
task - 任务get方法在成功完成时将会返回null结果。public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
当所有任务完成时,返回保持任务状态和结果的Future列表。 返回列表的所有元素的Future.isDone()为true。
注意,可以正常地或通过抛出异常来终止已完成任务。 如果正在进行此操作时修改了给定的 collection,则此方法的结果是不确定的。
T - 泛型tasks - 任务集合InterruptedException - 如果等待时发生中断,在这种情况下取消尚未完成的任务。public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
当所有任务完成或超时期满时(无论哪个首先发生),返回保持任务状态和结果的Future列表。 返回列表的所有元素的Future.isDone()为
true。 一旦返回后,即取消尚未完成的任务。 注意,可以正常地或通过抛出异常来终止已完成任务。 如果此操作正在进行时修改了给定的
collection,则此方法的结果是不确定的。
T - 泛型tasks - 任务集合timeout - 最长等待时间unit - 时间单位InterruptedException - 如果等待时发生中断,在这种情况下取消尚未完成的任务public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
如果某个任务已成功完成(也就是未抛出异常),则返回其结果。 一旦正常或异常返回后,则取消尚未完成的任务。 如果此操作正在进行时修改了给定的collection,则此方法的结果是不确定的。
T - 泛型tasks - 任务集合InterruptedException - 如果等待时发生中断ExecutionException - 如果没有任务成功完成public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
如果在给定的超时期满前某个任务已成功完成(也就是未抛出异常),则返回其结果。 一旦正常或异常返回后,则取消尚未完成的任务。 如果此操作正在进行时修改了给定的collection,则此方法的结果是不确定的。
T - 泛型tasks - 任务集合timeout - 最长等待时间unit - 时间单位InterruptedException - 如果等待时发生中断ExecutionException - 如果没有任务成功完成TimeoutException - 如果在所有任务成功完成之前给定的超时期满public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
command - 命令delay - 延迟时间unit - 单位get()方法在完成后将返回nullpublic <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
V - 泛型callable - 命令delay - 延迟时间unit - 时间单位public ScheduledFuture<?> scheduleWithFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
command - 命令initialDelay - 首次执行的延迟时间period - 连续执行之间的周期unit - 时间单位get()方法在取消后将抛出异常public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
command - 命令initialDelay - 首次执行的延迟时间delay - 每一次执行终止和下一次执行开始之间的延迟unit - 时间单位get()方法在取消后将抛出异常Copyright © 2021. All rights reserved.