Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ThreadFactoryBuilder in PackListener #14

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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