From 319ba07eb8d3d841d9b5b3ca98ac1dcce273f3b4 Mon Sep 17 00:00:00 2001 From: "Josiah (Gaming32) Glosson" Date: Fri, 12 Apr 2024 17:01:46 -0500 Subject: [PATCH] Use ThreadFactoryBuilder in PackListener --- .../geysermc/hydraulic/pack/PackListener.java | 25 ++++++------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/shared/src/main/java/org/geysermc/hydraulic/pack/PackListener.java b/shared/src/main/java/org/geysermc/hydraulic/pack/PackListener.java index 04f0ebd..127d534 100644 --- a/shared/src/main/java/org/geysermc/hydraulic/pack/PackListener.java +++ b/shared/src/main/java/org/geysermc/hydraulic/pack/PackListener.java @@ -1,5 +1,6 @@ package org.geysermc.hydraulic.pack; +import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.gson.Gson; import com.mojang.logging.LogUtils; import org.apache.commons.lang3.tuple.Pair; @@ -12,7 +13,6 @@ import org.geysermc.hydraulic.util.FormatUtil; import org.geysermc.hydraulic.util.PackUtil; import org.geysermc.pack.bedrock.resource.Manifest; -import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import java.io.IOException; @@ -26,8 +26,6 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.atomic.AtomicInteger; import java.util.zip.ZipFile; /** @@ -42,20 +40,13 @@ public class PackListener { private final PackManager manager; static { - int threads = Math.max(1, Runtime.getRuntime().availableProcessors() * 3 / 8); - THREAD_POOL = Executors.newFixedThreadPool(threads, new ThreadFactory() { - private final AtomicInteger threadCounter = new AtomicInteger(); - - @Override - public Thread newThread(final @NotNull Runnable run) { - Thread ret = new Thread(run); - - ret.setName(Constants.MOD_NAME + " Conversion Thread #" + this.threadCounter.getAndIncrement()); - ret.setUncaughtExceptionHandler((thread, throwable) -> LOGGER.error("Uncaught exception in thread " + thread.getName(), throwable)); - - return ret; - } - }); + THREAD_POOL = Executors.newFixedThreadPool( + Math.max(1, Runtime.getRuntime().availableProcessors() * 3 / 8), + new ThreadFactoryBuilder() + .setNameFormat(Constants.MOD_NAME + " Conversion Thread #%d") + .setUncaughtExceptionHandler((thread, throwable) -> LOGGER.error("Uncaught exception in thread {}", thread.getName(), throwable)) + .build() + ); } public PackListener(HydraulicImpl hydraulic, PackManager manager) {