diff --git a/src/main/java/ca/spottedleaf/moonrise/mixin/util_thread_counts/UtilMixin.java b/src/main/java/ca/spottedleaf/moonrise/mixin/util_thread_counts/UtilMixin.java index 248a5903..c13eb967 100644 --- a/src/main/java/ca/spottedleaf/moonrise/mixin/util_thread_counts/UtilMixin.java +++ b/src/main/java/ca/spottedleaf/moonrise/mixin/util_thread_counts/UtilMixin.java @@ -1,37 +1,23 @@ package ca.spottedleaf.moonrise.mixin.util_thread_counts; -import com.google.common.util.concurrent.MoreExecutors; import net.minecraft.Util; import net.minecraft.util.Mth; -import org.slf4j.Logger; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; -import java.util.concurrent.ExecutorService; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; import java.util.concurrent.ForkJoinPool; -import java.util.concurrent.ForkJoinWorkerThread; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; @Mixin(Util.class) abstract class UtilMixin { - @Shadow - @Final - static Logger LOGGER; - @Shadow private static int getMaxThreads() { return 0; } - @Shadow - private static void onThreadException(Thread thread, Throwable throwable) { - - } - @Unique private static int getThreadCounts(final int min, final int max) { final int cpus = Runtime.getRuntime().availableProcessors() / 2; @@ -55,29 +41,17 @@ private static int getThreadCounts(final int min, final int max) { * not lost due to some timeout, as G1GC has some issues cleaning up those. * @author Spottedleaf */ - @Overwrite - public static ExecutorService makeExecutor(final String name) { + @Redirect( + method = "makeExecutor", + at = @At( + value = "NEW", + target = "(ILjava/util/concurrent/ForkJoinPool$ForkJoinWorkerThreadFactory;Ljava/lang/Thread$UncaughtExceptionHandler;Z)Ljava/util/concurrent/ForkJoinPool;" + ) + ) + private static ForkJoinPool modifyExecutor(final int parallelism, final ForkJoinPool.ForkJoinWorkerThreadFactory factory, + final Thread.UncaughtExceptionHandler handler, final boolean asyncMode) { final int threads = getThreadCounts(1, getMaxThreads()); - if (threads <= 0) { - return MoreExecutors.newDirectExecutorService(); - } - - final AtomicInteger workerCount = new AtomicInteger(); - - return new ForkJoinPool(threads, (forkJoinPool) -> { - ForkJoinWorkerThread forkJoinWorkerThread = new ForkJoinWorkerThread(forkJoinPool) { - protected void onTermination(final Throwable throwable) { - if (throwable != null) { - LOGGER.error(this.getName() + " died", throwable); - } else { - LOGGER.debug(this.getName() + " shutdown"); - } - super.onTermination(throwable); - } - }; - forkJoinWorkerThread.setName("Worker-" + name + "-" + workerCount.getAndIncrement()); - return forkJoinWorkerThread; - }, UtilMixin::onThreadException, true, 0, Integer.MAX_VALUE, 1, null, 365, TimeUnit.DAYS); + return new ForkJoinPool(threads, factory, handler, asyncMode, 0, Integer.MAX_VALUE, 1, null, 365, TimeUnit.DAYS); } }