Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.rocketmq.common.constant.LoggerName;
import org.apache.rocketmq.logging.InternalLogger;
import org.apache.rocketmq.logging.InternalLoggerFactory;

public class ThreadFactoryImpl implements ThreadFactory {

private static final InternalLogger LOGGER = InternalLoggerFactory.getLogger(LoggerName.COMMON_LOGGER_NAME);

private final AtomicLong threadIndex = new AtomicLong(0);
private final String threadNamePrefix;
private final boolean daemon;
Expand Down Expand Up @@ -51,6 +57,12 @@ public ThreadFactoryImpl(final String threadNamePrefix, boolean daemon, BrokerId
public Thread newThread(Runnable r) {
Thread thread = new Thread(r, threadNamePrefix + this.threadIndex.incrementAndGet());
thread.setDaemon(daemon);

// Log all uncaught exception
thread.setUncaughtExceptionHandler((t, e) ->
LOGGER.error("[BUG] Thread has an uncaught exception, threadId={}, threadName={}",
t.getId(), t.getName(), e));

return thread;
}
}