All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.unistuttgart.isw.sfsc.commonjava.util.ExceptionLoggingThreadFactory Maven / Gradle / Ivy

package de.unistuttgart.isw.sfsc.commonjava.util;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import org.slf4j.Logger;

public class ExceptionLoggingThreadFactory implements ThreadFactory {

  private final ThreadGroup threadGroup;
  private final Supplier nameSupplier;
  private final UncaughtExceptionHandler uncaughtExceptionHandler;

  public ExceptionLoggingThreadFactory(String threadGroupName, Logger logger) {
    AtomicInteger number = new AtomicInteger();
    nameSupplier = () -> threadGroupName + "-thread-" + number.getAndIncrement();
    threadGroup = new ThreadGroup(threadGroupName);
    uncaughtExceptionHandler = (t, e) -> logger.warn("Uncaught Exception in thread " + t.getName(), e);
  }

  @Override
  public Thread newThread(Runnable r) {
    Thread thread = new Thread(threadGroup, r, nameSupplier.get());
    thread.setUncaughtExceptionHandler(uncaughtExceptionHandler);
    return thread;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy