一、FixedThreadPool、CachedThreadPool、SingleThreadExecutor、ScheduledThreadPool 简单使用(不推荐)
public void fixedThreadPool1(){
int size = 5;
ExecutorService threadPool = Executors.newFixedThreadPool(size);
// ExecutorService threadPool = Executors.newCachedThreadPool(); //缓存,同上
// ExecutorService threadPool = Executors.newSingleThreadExecutor(); //单线程
// ExecutorService threadPool = Executors.newScheduledThreadPool(size); //
while (size>0) {
threadPool.execute(new MyRunnable());
size--;
}
threadPool.shutdown();
}
class MyRunnable implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"运行了");
}
}
不推荐原因:
1)newFixedThreadPool和newSingleThreadExecutor:
主要问题是堆积的请求处理队列可能会耗费非常大的内存,甚至OOM。
2)newCachedThreadPool和newScheduledThreadPool:
主要问题是线程数最大数是Integer.MAX_VALUE,可能会创建数量非常多的线程,甚至OOM。
二、ExecutorService、ThreadPoolExecutor实例化用法(阿里巴巴规范)【推荐】
原理:使用第三方线程工厂,com.google.guava包
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0.1-jre</version>
</dependency>
public void alibabaThreadPool(){
//线程池核心池的大小
int corePoolSize = 5;
//线程池的最大线程数
int maximumPoolSize = 200;
//当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间
long keepAliveTime = 0L;
//keepAliveTime 的时间单位
TimeUnit unit = TimeUnit.MILLISECONDS;
//用来储存等待执行任务的队列
BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(1024);
//线程工厂,使用com.google.guava包
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("my-pool-%d").build();
//拒绝策略
RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
ExecutorService threadPool = new ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime,unit,workQueue,threadFactory,handler);
//这里为了备注各参数用途,就没有像下面优雅编写
/*ExecutorService threadPool = new ThreadPoolExecutor(5,200,0L,TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(1024),
new ThreadFactoryBuilder().setNameFormat("my-pool-%d").build(),new ThreadPoolExecutor.AbortPolicy());*/
while (corePoolSize>0) {
threadPool.execute(new MyRunnable());
corePoolSize--;
}
threadPool.shutdown();
}
三、Future、Callable有返回值,get()阻塞串行
原理:Future是ExecutorService 调用submit()返回的结果集(泛型),实现Callable接口,支持线程执行完返回结果。
/***
* 有返回,串行
* @throws Exception
*/
public void alibabaThreadPool2() throws Exception{
int corePoolSize = 5;
ExecutorService threadPool = new ThreadPoolExecutor(corePoolSize,200,0L,TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(1024),
n