常用线程池 及其API

1.newFixedThreadPool

99385a08b1e0a17dbd1c6625389135378839f626

2.newCachedThreadPool

适合任务分布密集,

但是耗时短的

2e91352ca9d0f943f1cff52cb3e52d46a78dd8d9

3.newSingleThreadExecutor

确保始终有一个线程可用,

并且有兜底策略

基于装饰者模式,通过强制类型转换只对外暴露接口,使得ThreadPoolExecutor的方法不可用

其参数不可变

ecc67d7f1a3a7d8bd0184212df6da5037ca0b204
```java ``` ```java ``` ```java ```
开启线程池提交任务 def5417532d57793aafd6a109715b863eb1eb494
1.execuate 无返回值 consumer
2.submit 有返回值

Future<T> submit(Callable<T>)

--->Callable接口有返回值重写call方法, 并且返回

  1. invokeAll 返回future集合

可以设置超时时间

<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException;
4.invokeAny
<T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException, ExecutionException;
结束线程
shutdown停止接收task, 但不停止正在运行的task
shutdownNow停止接受task, 且会停止正在云顶的task