From b077100667c1dee6e73da399e3484f92bbf67cb8 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 24 Apr 2022 17:25:47 +0100 Subject: Fix NPE caused by initialisation order on Forge/Fabric --- .../main/java/me/lucko/spark/common/util/SparkThreadFactory.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'spark-common/src/main/java/me') diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/SparkThreadFactory.java b/spark-common/src/main/java/me/lucko/spark/common/util/SparkThreadFactory.java index 9c7309d..156fa0d 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/SparkThreadFactory.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/SparkThreadFactory.java @@ -23,7 +23,7 @@ package me.lucko.spark.common.util; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; -public class SparkThreadFactory implements ThreadFactory { +public class SparkThreadFactory implements ThreadFactory, Thread.UncaughtExceptionHandler { private static final AtomicInteger poolNumber = new AtomicInteger(1); private final AtomicInteger threadNumber = new AtomicInteger(1); private final String namePrefix; @@ -36,7 +36,14 @@ public class SparkThreadFactory implements ThreadFactory { public Thread newThread(Runnable r) { Thread t = new Thread(r, this.namePrefix + this.threadNumber.getAndIncrement()); + t.setUncaughtExceptionHandler(this); t.setDaemon(true); return t; } + + @Override + public void uncaughtException(Thread t, Throwable e) { + System.err.println("Uncaught exception thrown by thread " + t.getName()); + e.printStackTrace(); + } } -- cgit