上一篇文章已经介绍了线程的基本概念以及线程相关的API,下面来看一下线程池
一、线程池框架
线程池的优点
- 重用线程池中的线程,避免因为线程的创建和销毁所带来的性能开销。
- 能有效控制线程池的最大并发数,避免大量线程之间因互相抢夺系统资源而导致的阻塞现象。
- 能够对线程进行简单的管理,并提供定时执行以及指向间隔循环执行等功能。
JAVA中相关类
线程池任务处理过程
线程池状态
- RUNNING:运行状态,线程池创建好之后就会进入此状态,如果不手动调用关闭方法,那么线程池在整个程序运行期间都是此状态。
- SHUTDOWN:关闭状态,不再接受新任务提交,但是会将已保存在任务队列中的任务处理完。
- STOP:停止状态,不再接受新任务提交,并且会中断当前正在执行的任务、放弃任务队列中已有的任务。
- TIDYING:整理状态,所有的任务都执行完毕后(也包括任务队列中的任务执行完),当前线程池中的活动线程数降为 0 时的状态。到此状态之后,会调用线程池的 terminated() 方法。
- TERMINATED:销毁状态,当执行完线程池的 terminated() 方法之后就会变为此状态。
ThreadPoolExecutor类源码
构造方法的七个参数
int corePoolSize,//核心线程数量
int maximumPoolSize,//最大线程数量
long keepAliveTime,//非核心线程空闲时间
TimeUnit unit,//非核心线程空闲时间的单位
BlockingQueue<Runnable> workQueue,//等待队列
ThreadFactory threadFactory,//线程工厂
RejectedExecutionHandler handler//拒绝策略
重要属性
private final BlockingQueue<Runnable> workQueue;//任务缓存队列,用来存放等待执行的任务
private final ReentrantLock mainLock = new ReentrantLock();//线程池的主要状态锁,对线程池状态(比如线程池大小、runState等)的改变都要使用这个锁
private final HashSet<Worker> workers = new HashSet<Worker>();//用来存放工作集
private final Condition termination = mainLock.newCondition();//
private int largestPoolSize;//用来记录线程池中曾经出现过的最大线程数
private long completedTaskCount;//用来记录已经执行完毕的任务个数
private volatile ThreadFactory threadFactory;//线程工厂,用来创建线程
private volatile RejectedExecutionHandler handler;//任务拒绝策略
private volatile long keepAliveTime;//线程存活时间
private volatile boolean allowCoreThreadTimeOut;//是否允许为核心线程设置存活时间
private volatile int corePoolSize;//核心池的大小(即线程池中的线程数目大于这个参数时,提交的任务会被放进任务缓存队列)
private volatile int maximumPoolSize;//线程池最大能容忍的线程数
private static final RejectedExecutionHandler defaultHandler =
new AbortPolicy();//默认的任务拒绝策略
private static final RuntimePermission shutdownPerm =
new RuntimePermission("modifyThread");
重要方法execute
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
else if (!addWorker(command, false))
reject(command);
}
线程池简单使用Demo
package org.cc.lipiao;
import java.time.LocalDateTime;
import java.util.concurrent.*;
import lombok.Getter;
public class ThreadPoolDemo {
private static volatile boolean flag = true;
public static void main(String[] args) throws InterruptedException {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,4,4,
TimeUnit.SECONDS,new ArrayBlockingQueue<>(6), Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy()
// new ThreadPoolExecutor.CallerRunsPolicy()
// new ThreadPoolExecutor.DiscardPolicy()
// new ThreadPoolExecutor.DiscardOldestPolicy()
);
for (int i = 0; i < 15; i++) {
MyThread thread = new MyThread("task" + i);
try{
threadPoolExecutor.execute(thread);
Thread.sleep(1000);
}
catch (Exception e){
System.out.println(String.format("任务%s提交后抛出异常,原因:%s",thread.getTaskName(),e.getMessage()));
flag = false;
}
finally {
System.out.println(String.format("核心线程数%d\n,最大线程数%d\n,达到过的最大线程数:%d\n,正在执行任务的线程数:%d\n" +
"当前池中的线程总数:%d\n已经完成任务数%d\n等待队列中的任务数%d\n",threadPoolExecutor.getCorePoolSize(),threadPoolExecutor.getMaximumPoolSize(),
threadPoolExecutor.getLargestPoolSize(),threadPoolExecutor.getActiveCount(),threadPoolExecutor.getPoolSize(),
threadPoolExecutor.getCompletedTaskCount(),threadPoolExecutor.getQueue().size()));
}
}
System.out.println("==================="+ LocalDateTime.now()+"=========================");
if(flag){
flag = false;
}
for (int i = 0; i < 5; i++) {
Thread.sleep(2000);
System.out.println(String.format("核心线程数%d\n,最大线程数%d\n,达到过的最大线程数:%d\n,正在执行任务的线程数:%d\n" +
"当前池中的线程总数:%d\n已经完成任务数%d\n等待队列中的任务数%d\n",threadPoolExecutor.getCorePoolSize(),threadPoolExecutor.getMaximumPoolSize(),
threadPoolExecutor.getLargestPoolSize(),threadPoolExecutor.getActiveCount(),threadPoolExecutor.getPoolSize(),
threadPoolExecutor.getCompletedTaskCount(),threadPoolExecutor.getQueue().size()));
}
threadPoolExecutor.shutdown();
}
@Getter
private static class MyThread extends Thread{
private String taskName;
public MyThread(String taskName) {
this.taskName = taskName;
}
@Override
public void run() {
if("main".equals(Thread.currentThread().getName())){
//当使用CallerRunsPolicy拒绝策略时,由主线程处理任务
flag = false;
}
while (flag){
}
System.out.println(String.format("任务%s由线程%s执行完毕",this.taskName,Thread.currentThread()));
}
}
}
简单使用Demo2
package com.demo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPool {
private int threadCount = 10;
private int threadpoolCount = 3;
public static void main(String[] args) {
new ThreadPool().threadPoolControl();
}
public void threadPoolControl() {
ThreadObject[] et = new ThreadObject[threadCount];
ExecutorService service = Executors.newFixedThreadPool(threadpoolCount);
Collection<ThreadObject> c = new ArrayList<ThreadObject>();
for (int i = 0; i < threadCount; i++) {
et[i] = new ThreadObject();
c.add(et[i]);
}
try {
service.invokeAll(c);
service.shutdown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
class ThreadObject implements Callable<Object>{
public Object call() throws Exception {
System.out.println("当前线程:"+Thread.currentThread().getName()+",线程对象:"+this);
return null;
}
}
}
线程池的分类
newFixedThreadPool
newCachedThreadPool
newScheduledThreadPool
newWorkStealingPool
newSingleThreadExecutor
newSingleThreadScheduledExecutor