s-day06-线程池的创建--方式一(建议使用)
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package com.inmind.threadpool10;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/*
|
||||
在java中使用一个接口表示线程池ExecutorService
|
||||
如何获取线程池对象??
|
||||
方式一:直接创建实现类ThreadPoolExecutor 的对象
|
||||
方式二:Executors(线程池)的静态方法
|
||||
|
||||
方式一的构造方法:
|
||||
ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
|
||||
TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)
|
||||
|
||||
参数一:corePoolSize,指定线程池核心线程的数量
|
||||
参数二:maximumPoolSize,指定线程池的最大线程数量
|
||||
参数三:keepAliveTime,指定临时线程的存活时间
|
||||
参数四:unit,指定临时线程存活时间单位(秒,分,时,天)
|
||||
参数五:workQueue 指定线程池的任务队列
|
||||
参数六:threadFactory,指定线程池的线程工厂
|
||||
参数七:handler,指定线程池的任务拒绝策略
|
||||
|
||||
|
||||
注意:
|
||||
1.临时线程什么时候会创建??
|
||||
新任务提交到线程池,发现所有的核心线程都被占用,任务队列也满了,并且还可以创建临时线程时,才会创建新的临时线程
|
||||
|
||||
2.什么时候会开始拒绝任务???
|
||||
当所有的核心线程和临时线程都在忙,而且任务对列也都满了,新的任务还来,线程池就开始拒绝任务
|
||||
*/
|
||||
public class Demo12 {
|
||||
public static void main(String[] args) {
|
||||
//线程池的多态
|
||||
ExecutorService threadpool = new ThreadPoolExecutor(
|
||||
3,
|
||||
5,
|
||||
8,
|
||||
TimeUnit.SECONDS,
|
||||
new ArrayBlockingQueue<>(4),
|
||||
Executors.defaultThreadFactory(),
|
||||
new ThreadPoolExecutor.AbortPolicy()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user