- 线程池一共是两大类,
ForkJoinPool
和ThreadPoolExecutor
通过
Executors
创建的线程池要么是ForkJoinPool
,要么就是不同参数值的ThreadPoolExecutor
Executor接口源码
public interface Executor {
void execute(Runnable command);
}
-
ExecutorService接口源码
```java public interface ExecutorService extends Executor {
/**
- 不接受新的任务,已接收的任务执行完毕结束 */ void shutdown();
/**
- 立即中断现有执行的线程
*/
List
shutdownNow();
/**
- Returns {@code true} if this executor has been shut down. *
- @return {@code true} if this executor has been shut down */ boolean isShutdown();
/*
- 要么shutdown调用,要么shutdownNow被调用,否则不会返回true */ boolean isTerminated();
/**
- Blocks until all tasks have completed execution after a shutdown
- request, or the timeout occurs, or the current thread is
- interrupted, whichever happens first.
- 阻塞当前线程,知道下面三者之一发生
- 1.在shutdown()后并且所有任务完成
- 2.指定等待时间超时了
- 3.当前线程被中断了 */ boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;
/**
- 实际底层还是调用execute(FutureTask)
*/
Future submit(Callable task);
/**
- */
Future submit(Runnable task, T result);
/**
- submit */ Future<?> submit(Runnable task);
/**
- */
List > invokeAll(Collection<? extends Callable > tasks) throws InterruptedException;
/**
- 所有任务结束后返回
*/
List > invokeAll(Collection<? extends Callable > tasks,
throws InterruptedException;long timeout, TimeUnit unit)
/* */
T invokeAny(Collection<? extends Callable > tasks) throws InterruptedException, ExecutionException; /**
- 任意一个任务接收就返回
*/
T invokeAny(Collection<? extends Callable > tasks,
throws InterruptedException, ExecutionException, TimeoutException; }long timeout, TimeUnit unit)
<a name="A3IlK"></a>
# AbstractExecutorService抽象类
- 抽象实现
<a name="dscMD"></a>
## newTaskFor()
```java
/**
* 均封装 FutureTask
*
*/
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
return new FutureTask<T>(runnable, value);
}
/**
* Returns a {@code RunnableFuture} for the given callable task.
*
*/
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
return new FutureTask<T>(callable);
}
submit()
submit
方法实际也是调用了execute
方法 ```javapublic Future<?> submit(Runnable task) { if (task == null) throw new NullPointerException(); RunnableFuture
ftask = newTaskFor(task, null); *execute(ftask); return ftask; }
public <T> Future<T> submit(Runnable task, T result) {
if (task == null) throw new NullPointerException();
RunnableFuture<T> ftask = newTaskFor(task, result);
*execute(ftask);
return ftask;
}
public <T> Future<T> submit(Callable<T> task) {
if (task == null) throw new NullPointerException();
RunnableFuture<T> ftask = newTaskFor(task);
*execute(ftask);
return ftask;
}
<a name="bqMyB"></a>
# ThreadPoolExecutor类
具体参数含义之前写过。
<a name="pcHfC"></a>
# Executors各种线程池特点
```java
@Test
public void testAllPool(){
// 0队列,0核心线程,MAX_VALUE临时线程,适用于处理时间短但任务多的
Executors.newCachedThreadPool();
// 无限队列,5核心 0临时 适用于长任务
Executors.newFixedThreadPool(5);
// 无限队列,1核心 0临时 是特殊的FixedThreadPool
Executors.newSingleThreadExecutor();
// 延时队列,2核心 MAX_VALUE临时线程,定时任务线程池
Executors.newScheduledThreadPool(2);
// 核心线程为1的定时任务线程池
Executors.newSingleThreadScheduledExecutor();
// ForkJoinPool
Executors.newWorkStealingPool();
}
线程池状态轮转
- RUNING->SHUTDOWN:调用了
shutdown()
- RUNING ->STOP: 显式调用
shutdownNow()
- SHUTDOWN->STOP:显式调用
shutdownNow()
- SHUTDOWN->TIDYING: 无工作线程和队列为空时
- STOP->TIDYING: 无工作线程时
- TIDYING->TERMINATED: 调用
terminated()
方法时