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

shz.core.timewheel.BSTQueueSingleTimeWheel Maven / Gradle / Ivy

package shz.core.timewheel;

import shz.core.ThreadHelp;
import shz.core.queue.l.ConcurrentLLinkedQueue;
import shz.core.st.bst.ixx.ConcurrentILRedBlackBST;

import java.util.Iterator;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;

public abstract class BSTQueueSingleTimeWheel extends SingleTimeWheel {
    private final Executor workExecutor;

    public BSTQueueSingleTimeWheel(String threadName, int slotSeconds, Executor workExecutor) {
        super(threadName, slotSeconds);
        this.workExecutor = workExecutor;
    }

    public BSTQueueSingleTimeWheel(int slotSeconds) {
        super(slotSeconds);
        workExecutor = ThreadHelp.getExecutor(ThreadHelp.TPConfig
                .of(threadName + "- WorkExecutor")
                .tpType(ThreadHelp.TPType.FIXED_THREAD_POOL)
                .daemon(true)
                .workQueue(new LinkedBlockingQueue<>(1 << 27)));
    }

    public BSTQueueSingleTimeWheel() {
        this(1);
    }

    protected final ConcurrentILRedBlackBST> queues = ConcurrentILRedBlackBST.of(-1);

    @Override
    protected final boolean acceptable() {
        return queues.size() > 1;
    }

    @Override
    protected final void execute(int slot) {
        ConcurrentLLinkedQueue queue = queues.get(slot);
        if (queue != null) {
            queues.delete(slot);
            workExecutor.execute(() -> execute(queue.iterator()));
        }
    }

    protected abstract void execute(Iterator iterator);

    @Override
    protected void offer(int slot, T t) {
        queues.computeIfAbsent(slot, k -> ConcurrentLLinkedQueue.of()).offer(t);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy