Remove duplicate thread initialization

This commit is contained in:
filoghost 2024-03-09 22:51:16 +01:00
parent 0754e0ac33
commit 9dcf6035fe

View File

@ -13,37 +13,21 @@ import java.util.concurrent.LinkedBlockingQueue;
/** /**
* This is a quick but ugly helper class for creating and sending packets async. * This is a quick but ugly helper class for creating and sending packets async.
* Static classes like this should be avoided. * Static classes like this should be avoided.
* Important note: this executor can only have one thread, as packets must be sent in a precise order.
*/ */
public class PacketSenderExecutor { public class PacketSenderExecutor {
private static volatile BlockingQueue<Runnable> tasks; private static volatile BlockingQueue<Runnable> tasks;
private static volatile Thread thread;
private static final Runnable STOP_MARKER_TASK = () -> {}; private static final Runnable STOP_MARKER_TASK = () -> {};
static {
tasks = new LinkedBlockingQueue<>();
thread = new Thread(() -> {
while (true) {
try {
Runnable task = tasks.take();
task.run();
} catch (Throwable t) {
Log.severe("Error in packet sender task", t);
}
}
});
thread.setName("Holographic Displays async packets");
thread.start();
}
public static void execute(Runnable task) { public static void execute(Runnable task) {
tasks.add(task); tasks.add(task);
} }
public static void start() { public static void start() {
tasks = new LinkedBlockingQueue<>(); tasks = new LinkedBlockingQueue<>();
thread = new Thread(() -> { Thread thread = new Thread(() -> {
while (true) { while (true) {
try { try {
Runnable task = tasks.take(); Runnable task = tasks.take();
@ -56,7 +40,7 @@ public class PacketSenderExecutor {
} }
} }
}); });
thread.setName("Holographic Displays packet sender"); thread.setName("Holographic Displays async packets");
thread.start(); thread.start();
} }