Added repeating tasks to TaskQueue

This commit is contained in:
OLEGSHA 2020-12-16 17:05:12 +03:00
parent 0a8db1cd02
commit bfd5c02b0c
3 changed files with 28 additions and 10 deletions

View File

@ -24,6 +24,14 @@ public class RenderTaskQueue {
private static final TaskQueue HANDLER = private static final TaskQueue HANDLER =
new TaskQueue(GraphicsInterface::isRenderThread); new TaskQueue(GraphicsInterface::isRenderThread);
public static void schedule(Runnable task) {
HANDLER.schedule(task);
}
public static void removeScheduled(Runnable task) {
HANDLER.removeScheduled(task);
}
public static void invokeLater(Runnable task) { public static void invokeLater(Runnable task) {
HANDLER.invokeLater(task); HANDLER.invokeLater(task);

View File

@ -17,6 +17,7 @@
*******************************************************************************/ *******************************************************************************/
package ru.windcorp.progressia.common.util; package ru.windcorp.progressia.common.util;
import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
@ -29,11 +30,21 @@ import ru.windcorp.jputil.functions.ThrowingRunnable;
public class TaskQueue { public class TaskQueue {
private final Queue<Runnable> queue = new ConcurrentLinkedQueue<>(); private final Queue<Runnable> queue = new ConcurrentLinkedQueue<>();
private final Collection<Runnable> repeating = new ConcurrentLinkedQueue<>();
private final BooleanSupplier runNow; private final BooleanSupplier runNow;
public TaskQueue(BooleanSupplier runNow) { public TaskQueue(BooleanSupplier runNow) {
this.runNow = runNow; this.runNow = runNow;
} }
public void schedule(Runnable task) {
repeating.add(task);
}
public void removeScheduled(Runnable task) {
repeating.remove(task);
}
public void invokeLater(Runnable task) { public void invokeLater(Runnable task) {
queue.add(task); queue.add(task);
@ -106,6 +117,10 @@ public class TaskQueue {
tasks.next().run(); tasks.next().run();
tasks.remove(); tasks.remove();
} }
for (Runnable task : repeating) {
task.run();
}
} }
} }

View File

@ -1,8 +1,5 @@
package ru.windcorp.progressia.server; package ru.windcorp.progressia.server;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -34,7 +31,6 @@ public class Server {
private final ClientManager clientManager = new ClientManager(this); private final ClientManager clientManager = new ClientManager(this);
private final TaskQueue taskQueue = new TaskQueue(this::isServerThread); private final TaskQueue taskQueue = new TaskQueue(this::isServerThread);
private final Collection<Consumer<Server>> repeatingTasks = Collections.synchronizedCollection(new ArrayList<>());
private final TickingSettings tickingSettings = new TickingSettings(); private final TickingSettings tickingSettings = new TickingSettings();
@ -42,7 +38,7 @@ public class Server {
this.world = new WorldLogic(world, this); this.world = new WorldLogic(world, this);
this.serverThread = new ServerThread(this); this.serverThread = new ServerThread(this);
invokeEveryTick(this::scheduleChunkTicks); schedule(this::scheduleChunkTicks);
} }
/** /**
@ -80,7 +76,7 @@ public class Server {
* *
* @param task the task to run * @param task the task to run
* @see #invokeNow(Runnable) * @see #invokeNow(Runnable)
* @see #invokeEveryTick(Consumer) * @see #schedule(Consumer)
*/ */
public void invokeLater(Runnable task) { public void invokeLater(Runnable task) {
taskQueue.invokeLater(task); taskQueue.invokeLater(task);
@ -98,7 +94,7 @@ public class Server {
* *
* @param task the task to run * @param task the task to run
* @see #invokeLater(Runnable) * @see #invokeLater(Runnable)
* @see #invokeEveryTick(Consumer) * @see #schedule(Consumer)
*/ */
public void invokeNow(Runnable task) { public void invokeNow(Runnable task) {
taskQueue.invokeNow(task); taskQueue.invokeNow(task);
@ -110,8 +106,8 @@ public class Server {
taskQueue.waitAndInvoke(task); taskQueue.waitAndInvoke(task);
} }
public void invokeEveryTick(Consumer<Server> task) { public void schedule(Consumer<Server> task) {
repeatingTasks.add(task); taskQueue.schedule(() -> task.accept(this));
} }
public void requestChange(Change change) { public void requestChange(Change change) {
@ -161,7 +157,6 @@ public class Server {
*/ */
public void tick() { public void tick() {
taskQueue.runTasks(); taskQueue.runTasks();
repeatingTasks.forEach(t -> t.accept(this));
} }
/** /**