Executor框架的核心是线程池。线程池是指管理一组同构工作线程的资源池,具体可参考:https://www.cnblogs.com/timlearn/p/4023394.html
https://blog.csdn.net/pozmckaoddb/article/details/51478017
https://www.cnblogs.com/zhaoyan001/p/7049627.html
ThreadPoolExecutor继承自AbstractExecutorService,AbstractExecutorService实现了ExecutorService接口,所以ThreadPoolExecutor也间接实现了ExecutorService接口, ThreadPoolExecutor定义了很多构造函数,以下代码给出了该类最重要的构造函数:
public ThreadPoolExecutor(int corePoolSize,//线程池的目标大小,即在没有任务执行时线程池的大小,并且只有在工作队列满了的情况下才会创建超出这个数量的线程
int maximumPoolSize,//线程池的最大大小,表示线程池可同时活动线程数量上限。
long keepAliveTime,//线程池的存活时间
TimeUnit unit,//keepAliveTime和unit共同定义了线程的存活时间,如果某个线程的空闲时间超过了存活时间,那么将被标记为可回收的,并且当线程池的当前大小超过基本大小时,这个线程将被终止。
BlockingQueue<Runnable> workQueue,//workQueue参数包含Runnable的阻塞队列,当线程池达到基本大小时,新提交的任务将放入这个阻塞队列中,阻塞队列的实现包含三种:无界队列、有界队列以及同步移交队列。
ThreadFactory threadFactory,// threadFactory参数用于设置创建线程的工厂,可以通过线程工厂给每个创建出来的线程设置更有意义的名字,方便定位问题。
RejectedExecutionHandler handler//handler参数定义了线程池饱和策略。当有界队列被填满后,并且线程池活动线程达到最大线程数,饱和策略开始生效。
) { ... }
下面看实例,1.新建一个Executor线程池:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
*
* @Description:Executor线程池业务共用
* @author
* @date 2018年10月8日
*/
public class ThreadPool {
//根据系统资源进行设置线程池的大小
private static final int NCPU = Runtime.getRuntime().availableProcessors();
//
private static final ExecutorService executor = new ThreadPoolExecutor(
NCPU, NCPU * 2, 10L, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(200),
new ThreadPoolExecutor.CallerRunsPolicy());
/**
* 添加线程
* @param notice
*/
public static void addNotice(Runnable notice) {
executor.execute(notice);
}
}
2.新建一个异步线程:
import com./.bo.PlateBlackUserBO;
import com./.po.PlateBlackList;
import com./.service.PlateBlackListService;
import com./.spring.AppContext;
import java.util.Date;
/**
* @author
* @Description:异步线程判断车牌是否添加到黑名单用户
* @date 2018年10月31日 11:06
*/
public class ArrearsBlackAddThread implements Runnable {
// 车牌号码
private String plateNo;
// 车牌颜色
private Byte plateColor;
// 欠费次数配置
private Integer blackTimes;
// 欠费总额配置
private Integer blackMoney;
public ArrearsBlackAddThread(String plateNo, Byte plateColor,
Integer blackTimes, Integer blackMoney) {
super();
this.plateNo = plateNo;
this.plateColor = plateColor;
this.blackTimes = blackTimes;
this.blackMoney = blackMoney;
}
@Override
public void run() {
PlateBlackListService service = AppContext
.getBean("PlateBlackListServiceImpl");
// 查询车牌用户的欠费次数和欠费总额
PlateBlackUserBO userBO = service.queryPlateUserCountAndMoney(plateNo,
plateColor);
// 欠费次数或欠费总额有一个条件满足则添加到黑名单用户
if (userBO.getArrearsCount() >= blackTimes
|| userBO.getArrearsMoney() >= blackMoney) {
PlateBlackList record = new PlateBlackList();
record.setPlateNo(plateNo);
record.setPlateColor(plateColor);
record.setCreateTime(new Date());
record.setRemark("逾期车主");
service.savePlateBlackSelective(record);
}
}
}
3.调用(线程池)异步线程:
//开启黑名单则添加到线程池处理
if("1".equals(blackSwitch)){
ThreadPool.addNotice(new ArrearsBlackAddThread(plateNo,plateColor,Integer.valueOf(blackTimes),Integer.valueOf(blackMoney)));
}