任务处理类
package com.deepblue.aivideo.biz.thread;
import com.deepblue.aivideo.api.entiy.DeviceAbilityGroupRelationDto;
import com.deepblue.aivideo.api.entiy.PersonFaceLog;
import com.deepblue.aivideo.biz.service.PersonFaceLogService;
import com.deepblue.aivideo.biz.socket.DeviceAndLogSocketServer;
import com.deepblue.core.util.UUIDUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
@Data
@Slf4j
@Component
@Scope("prototype")
public class BusinessThread implements Runnable{
private 参数1 参数1;
private 参数2 参数2;
public BusinessThread(参数1,参数2) {
this.参数1=参数1;
this.参数2=参数2;
}
@Override
public void run() {
//执行每个任务的处理
}
}
任务管理
package com.deepblue.aivideo.biz.thread;
import com.deepblue.aivideo.api.entiy.DeviceAbilityGroupRelationDto;
import com.deepblue.aivideo.api.entiy.PersonFaceLog;
import com.deepblue.aivideo.biz.service.PersonFaceLogService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.*;
@Component
public class ThreadPoolManager implements BeanFactoryAware {
/**
* 用于从IOC里取对象
*/
private BeanFactory factory;
/**
* 线程池维护线程的最少数量
*/
private final static int CORE_POOL_SIZE = 10;
/**
* 线程池维护线程的最大数量
*/
private final static int MAX_POOL_SIZE = 20;
/**
* 线程池维护线程所容许的空闲时间
*/
private final static int KEEP_ALIVE_TIME = 0;
/**
* 线程池所使用的缓冲队列大小
*/
private final static int WORK_QUEUE_SIZE = 500;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
factory = beanFactory;
}
/**
* 订单的缓冲队列,当线程池满了,则将订单存入到此缓冲队列
*/
Queue<DeviceAbilityGroupRelationDto> msgQueue = new LinkedBlockingQueue<DeviceAbilityGroupRelationDto>();
/**
* 当线程池的容量满了,执行下面代码,将订单存入到缓冲队列
*/
final RejectedExecutionHandler handler = new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
//订单加入到缓冲队列
msgQueue.offer(((BusinessThread) r).getDeviceAbilityGroupRelationDto());
}
};
/**创建线程池
*
* 概念解释及原理(无实例):
* https://uule.iteye.com/blog/1123185
* https://www.cnblogs.com/trust-freedom/p/6594270.html
* https://www.cnblogs.com/zedosu/p/6665306.html
*
*含实例的:
* https://blog.csdn.net/x631617479/article/details/83001198 :自定义连接池ThreadPoolExecutor执行顺序
* https://blog.csdn.net/changyuan101/article/details/50755157 :ThreadPoolExecutor自定义RejectedExecutionHandler当队列满时改为调用BlockingQueue. put来实现生产者的阻塞
*
* 处理任务的优先级为:
* corePoolSize、任务队列workQueue、最大线程maximumPoolSize,如果三者都满了,使用(调度线程池)handler处理被拒绝的任务。
*
*
* */
final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
// 线程池维护线程的最少数量
CORE_POOL_SIZE,
// 线程池维护线程的最大数量
MAX_POOL_SIZE,
// 线程池维护线程所允许的空闲时间
KEEP_ALIVE_TIME,
// 线程池维护线程所允许的空闲时间的单位
TimeUnit.SECONDS,
// 线程池所使用的缓冲队列
new ArrayBlockingQueue(WORK_QUEUE_SIZE),
// 线程池对拒绝任务的处理策略
this.handler
);
/**
* 将任务加入订单线程池
*
* @param deviceAbilityGroupRelationDto 设备能力组关系dto
*/
public void addTask(参数){
//验证当前进入的订单是否已经存在
BusinessThread businessThread = new BusinessThread(参数1,参数2);
threadPool.execute(businessThread);
}
/**
* 线程池的定时任务----> 称为(调度线程池)。此线程池支持 定时以及周期性执行任务的需求。
*/
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5);
/**
* 检查(调度线程池),每秒执行一次,查看订单的缓冲队列是否有 订单记录,则从新加入到线程池
*/
final ScheduledFuture scheduledFuture = scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
//判断缓冲队列是否存在记录
if(!msgQueue.isEmpty()){
//当线程池的队列容量少于WORK_QUEUE_SIZE,则开始把缓冲队列的任务 加入到 线程池
if (threadPool.getQueue().size() < WORK_QUEUE_SIZE) {
BusinessThread businessThread = new BusinessThread(参数1,参数2);
threadPool.execute(businessThread);
}
}
}
}, 0, 1, TimeUnit.SECONDS);
/**
* 获取消息缓冲队列
*
* @return {@link Queue<Object>}
*/
public Queue<DeviceAbilityGroupRelationDto> getMsgQueue() {
return msgQueue;
}
/**
* 终止线程池+调度线程池
*/
public void shutdown() {
//true表示若是定时任务在执行,当即停止,false则等待任务结束后再中止
System.out.println("终止线程池+调度线程池:"+scheduledFuture.cancel(false));
scheduler.shutdown();
threadPool.shutdown();
}
}
调用
@Autowired
ThreadPoolManager threadPoolManager;
threadPoolManager.addTask(deviceAbilityGroupRelationDto);