All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gettyio.gim.utils.expiremap.BaseExpireCheckTask Maven / Gradle / Ivy

package com.gettyio.gim.utils.expiremap;

import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author gogym.ggj
 * @version 1.0.0
 * @ClassName BaseExpireCheckTask.java
 * @email [email protected]
 * @Description TODO
 * @createTime 2021/02/23/ 15:18:00
 */
abstract class BaseExpireCheckTask extends Thread {

    ConcurrentHashMap delayMap;
    ConcurrentHashMap expireMap;

    private final ReentrantLock lock;
    private final Condition cond;

    public BaseExpireCheckTask(ConcurrentHashMap expireMap, ConcurrentHashMap delayMap, ReentrantLock lock, Condition cond) {
        this.delayMap = delayMap;
        this.expireMap = expireMap;
        this.lock = lock;
        this.cond = cond;
    }

    protected abstract void expireEvent(K key, V val);

    @Override
    public void run() {
        while (true) {
            lock.lock();
            try {
                if (delayMap == null || delayMap.isEmpty()) {
                    try {
                        cond.await();
                    } catch (InterruptedException e) {
                        cond.signal();
                        continue;
                    }
                }
                Iterator it = delayMap.keySet().iterator();
                while (it.hasNext()) {
                    K key = it.next();
                    // 元素超时
                    if (delayMap.get(key) <= System.currentTimeMillis()) {
                        // 触发回调
                        expireEvent(key, expireMap.get(key));
                        // 移除
                        it.remove();
                        expireMap.remove(key);
                        delayMap.remove(key);
                    }
                }
            } finally {
                lock.unlock();
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy