fix: Fabric thread exhaustion/deadlock causing crashes (#661)

Attempt to fix thread issue on Fabric
This commit is contained in:
Vince authored and GitHub committed 2024-07-19 17:31:07 +01:00
1 parent b03e71fb43
commit 38df119d2e
1 file changed
+13 -12
@@ -19,18 +19,21 @@
package net.william278.huskhomes.util;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import net.william278.huskhomes.FabricHuskHomes;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.user.OnlineUser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
public interface FabricTask extends Task {
ScheduledExecutorService ASYNC_EXEC = Executors.newScheduledThreadPool(4,
new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("HuskHomes-ThreadPool")
.build());
class Sync extends Task.Sync implements FabricTask {
@@ -47,11 +50,9 @@ public interface FabricTask extends Task {
@Override
public void run() {
if (!cancelled) {
Executors.newSingleThreadScheduledExecutor().schedule(
() -> ((FabricHuskHomes) getPlugin()).getMinecraftServer().executeSync(runnable),
ASYNC_EXEC.schedule(() -> ((FabricHuskHomes) getPlugin()).getMinecraftServer().executeSync(runnable),
delayTicks * 50,
TimeUnit.MILLISECONDS
);
TimeUnit.MILLISECONDS);
}
}
}
@@ -74,7 +75,7 @@ public interface FabricTask extends Task {
@Override
public void run() {
if (!cancelled) {
this.task = CompletableFuture.runAsync(runnable, ((FabricHuskHomes) getPlugin()).getMinecraftServer());
this.task = CompletableFuture.runAsync(runnable, ASYNC_EXEC);
}
}
}
@@ -98,12 +99,11 @@ public interface FabricTask extends Task {
@Override
public void run() {
if (!cancelled) {
this.task = Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(
this.task = ASYNC_EXEC.scheduleAtFixedRate(
runnable,
0,
repeatingTicks * 50,
TimeUnit.MILLISECONDS
);
TimeUnit.MILLISECONDS);
}
}
}
@@ -130,6 +130,7 @@ public interface FabricTask extends Task {
@Override
default void cancelTasks() {
ASYNC_EXEC.shutdownNow();
}
}