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

de.eldoria.eldoutilities.scheduling.SelfSchedulingWorker Maven / Gradle / Ivy

There is a newer version: 2.1.6
Show newest version
/*
 *     SPDX-License-Identifier: LGPL-3.0-or-later
 *
 *     Copyright (C) EldoriaRPG Team and Contributor
 */

package de.eldoria.eldoutilities.scheduling;

import de.eldoria.eldoutilities.threading.ReschedulingTask;
import org.bukkit.plugin.Plugin;

import java.util.Collection;

/**
 * A self scheduling worker which will schedule itself when getting tasks.
 * 

* Will unschedule itself if no tasks are left for some time. * * @param type of collection * @param type of collection implementation * @since 1.4.0 */ public abstract class SelfSchedulingWorker> extends ReschedulingTask { private final T tasks; private int idleTicks; private int maxIdleTicks = 200; public SelfSchedulingWorker(Plugin plugin, int maxIdleTicks) { this(plugin); this.maxIdleTicks = maxIdleTicks; } public SelfSchedulingWorker(Plugin plugin) { super(plugin); tasks = getQueueImplementation(); } /** * handle one object which was polled from the queue * * @param object object from queue */ protected abstract void execute(V object); /** * Tick is executed once per tick. */ protected void tick() { } @Override public final void run() { if (!tasks.isEmpty()) { tick(); for (var task : tasks) { execute(task); } } else { idleTicks++; if (idleTicks >= maxIdleTicks) { cancel(); } } } protected final void register(V object) { if (!isActive()) return; tasks.add(object); if (!isRunning()) { schedule(); } idleTicks = 0; } public final void unregister(V object) { tasks.remove(object); } protected abstract T getQueueImplementation(); @Override public final void shutdown() { super.shutdown(); for (var task : tasks) { execute(task); } tasks.clear(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy