意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

java 实现满足一定数量超过一定时间后才做任务的队列

来源:恒创科技 编辑:恒创科技编辑部
2024-01-23 03:44:59
@Slf4j
public class EventDelayQueueService {

    private final List<TaskInfo> items = Collections.synchronizedList(new ArrayList<>());
    private final int maxCapacity = 500;
    private final ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1);;
    private final MDCThreadPoolExecutor executor = Executors.newFixedThreadPool(5);;
    private final AtomicInteger count = new AtomicInteger(0);

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("start the EventDelayQueueService .");
        scheduled.scheduleAtFixedRate(() -> {
            if (items.size() > maxCapacity) {
                asyncAction();
                return;
            }
            if (count.incrementAndGet() > 10) {
                asyncAction();
            }
        },10,200, TimeUnit.MILLISECONDS);
    }

    private void asyncAction() {
        // TODO
    }


    public void add(TaskInfo info) {
        items.add(info);
    }
}


java 实现满足一定数量超过一定时间后才做任务的队列

上一篇: 详解ConCurrentHashMap源码(jdk1.8) 下一篇: 手机怎么远程登录云服务器?