Skip to content

Commit

Permalink
Use ThreadFactoryBuilder in PackListener (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 authored Apr 12, 2024
1 parent 35fbfa2 commit 5f04d88
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions shared/src/main/java/org/geysermc/hydraulic/pack/PackListener.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;

/**
Expand All @@ -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) {
Expand Down

0 comments on commit 5f04d88

Please sign in to comment.