java轻型内存队列处理demo

发布时间 2023-05-31 11:18:05作者: 雨飞
java轻型内存队列处理demo

@Component public class ConcurrentLinkedQueueUtils { static AtpLogBiz atpLogBiz; static AuditLogtTmpDataService auditLogDataService; static ConcurrentLinkedQueue conList = new ConcurrentLinkedQueue(); private static volatile Boolean CLEAN_THREAD_IS_RUN = false; @Autowired public ConcurrentLinkedQueueUtils(AuditLogtTmpDataService client,AtpLogBiz biz) { ConcurrentLinkedQueueUtils.auditLogDataService = client; ConcurrentLinkedQueueUtils.atpLogBiz = biz; ConcurrentLinkedQueueUtils.startCleanThread(); } public static void addNode(AddAtpLogReq node) { conList.offer(node); } public static void handleNode() { List<AuditLogModel> auditList = new ArrayList<>(); while (!conList.isEmpty()) { AddAtpLogReq atpLogReq = (AddAtpLogReq) conList.poll(); //处理逻辑 } private static AuditLogModel convertErrorLog(AddAtpLogReq atpLogReq) { } public static void startCleanThread() { if (!CLEAN_THREAD_IS_RUN) { CleanLinkedNode cleanTimeOutThread = new CleanLinkedNode(); Thread thread = new Thread(cleanTimeOutThread); //设置为后台守护线程 thread.setDaemon(true); thread.start(); } } static void setCleanThreadRun() { CLEAN_THREAD_IS_RUN = true; } } /** * 每一分钟清理一次过时缓存 */ class CleanLinkedNode implements Runnable{ @Override public void run() { while (true) { ConcurrentLinkedQueueUtils.setCleanThreadRun(); ConcurrentLinkedQueueUtils.handleNode(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }