吉安感知网项目-后端
xiebin
2026-01-06 d207a86cdf1ab52ef8cb7cd83bad8fceab8038cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package org.sxkj.common.utils;
 
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
 
/**
 * 统一间隔执行任务
 */
public class NumHashMapRecordUtils {
    /**
     * 间隔次数map
     */
    private static Map<String, AtomicInteger> INTERVAL_NUM_MAP = new ConcurrentHashMap();
 
 
    /**
     * 根据次数执行
     *
     * @param id          唯一值
     * @param intervalNum 间隔执行次数
     * @param supplier    执行方法
     */
    public static void applyByNum(String id, Integer intervalNum, Runnable supplier) {
        AtomicInteger atomicInteger = Optional.ofNullable(INTERVAL_NUM_MAP.get(id)).orElse(new AtomicInteger(0));
 
        if (atomicInteger.get() % intervalNum == 0) {
            supplier.run();
        }
        atomicInteger.incrementAndGet();
        INTERVAL_NUM_MAP.put(id, atomicInteger);
    }
 
 
}