线程池接口:ExecutorService
创建线程池
-
ThreadPoolExecutor
public ThreadPoolExecutro(
int corePoolSize, //正式工
int maximumPollSize, //最大员工
long keepAliveTime, //存活时间
TimeUnit unit, //TimeUnit.SECONDS
BlockingQueue workQueue, //new ArrayBlockingQueue<>(3)
ThreadFactory threadFactory, //Executors.defaultThreadFactory()
RejectedExecutionHandler handler //new ThreadPoolExecutor.AbortPolicy()
)
ExecutorService pool = new ThreadPoolExecutro(
int corePoolSize, //正式工
int maximumPollSize, //最大员工
long keepAliveTime, //存活时间
TimeUnit unit, //TimeUnit.SECONDS
BlockingQueue workQueue, //new ArrayBlockingQueue<>(3)
ThreadFactory threadFactory, //Executors.defaultThreadFactory()
RejectedExecutionHandler handler //new ThreadPoolExecutor.AbortPolicy()
)
Runnable target = new MyRunnable;
pool.execute(target);
pool.shutdown();
pool.shutdownNow();
四种任务拒绝策略
-
Executors
不推荐使用
并发/并行
- 并发 线程
- 并行 逻辑处理器