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

overflowdb.util.NamedThreadFactory Maven / Gradle / Ivy

There is a newer version: 1.173
Show newest version
package overflowdb.util;

import org.slf4j.MDC;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadFactory;

public class NamedThreadFactory implements ThreadFactory {
  private final String threadName;
  private final Map mdc;

  public NamedThreadFactory(String threadName) {

    this.threadName = threadName;
    Map mdcTmp = MDC.getCopyOfContextMap();
    //logback chokes on null-maps
    this.mdc = mdcTmp != null ? mdcTmp : new HashMap<>();
  }

  public Thread newThread(Runnable r) {
    return new Thread(() -> {MDC.setContextMap(mdc); r.run();}, threadName);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy