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

com.groupbyinc.common.util.DefaultForkJoinWorkerThreadFactory Maven / Gradle / Ivy

There is a newer version: 198
Show newest version
package com.groupbyinc.common.util;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinWorkerThread;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * ThreadFactory that keeps the current thread's name to make logs easier to follow and grep
 */
public class DefaultForkJoinWorkerThreadFactory implements ForkJoinPool.ForkJoinWorkerThreadFactory {

  private static class WorkerThread extends ForkJoinWorkerThread {

    private WorkerThread(ForkJoinPool pool) {
      super(pool);
    }
  }

  private final AtomicInteger threadNumber = new AtomicInteger(0);
  private String parent;
  private String name;
  private boolean includeThreadNumber;

  public DefaultForkJoinWorkerThreadFactory(String parent, String name, boolean includeThreadNumber) {
    this.parent = parent;
    this.name = name;
    this.includeThreadNumber = includeThreadNumber;
  }

  @Override
  public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
    ForkJoinWorkerThread t = new WorkerThread(pool);
    StringBuilder overrideName = new StringBuilder();
    overrideName.append(parent);
    overrideName.append(":");
    overrideName.append(name);
    if (includeThreadNumber) {
      overrideName.append(":");
      overrideName.append(threadNumber.incrementAndGet());
    }
    t.setName(overrideName.toString());
    return t;
  }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy