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

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

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * Thread utilities
 *
 * @author Alan Czajkowski
 */
public final class ThreadUtils {

  private static final transient Logger LOG = LoggerFactory.getLogger(ThreadUtils.class);

  private static final int MAX_PARALLELISM = Math.max(1, Runtime.getRuntime().availableProcessors() - 1);

  private ThreadUtils() {
    // not publicly instantiable
  }

  public static void sleep(long millis) {
    try {
      Thread.sleep(millis);
    } catch (InterruptedException ie) {
      LOG.warn(ie.getMessage(), ie);
    }
  }

  public static ThreadFactory defaultThreadFactory(String name) {
    return defaultThreadFactory(name, false);
  }

  public static ThreadFactory defaultThreadFactory(String name, boolean includeThreadNumber) {
    return defaultThreadFactory(name, includeThreadNumber, true);
  }

  public static ThreadFactory defaultThreadFactory(String name, boolean includeThreadNumber, boolean includeParentThreadName) {
    return new DefaultThreadFactory(name, includeThreadNumber, includeParentThreadName);
  }

  public static ForkJoinPool newForkJoinPool(String name, boolean includeThreadName) {
    return newForkJoinPool(MAX_PARALLELISM, name, includeThreadName);
  }

  public static ForkJoinPool newForkJoinPool(int parallelism, String name, boolean includeThreadName) {
    ForkJoinPool.ForkJoinWorkerThreadFactory factory = new DefaultForkJoinWorkerThreadFactory(Thread.currentThread().getName(), name, includeThreadName);
    return new ForkJoinPool(Math.max(1, parallelism), factory, null, false);
  }

  public static ExecutorService newFixedPool(int parallelism, String name, boolean includeThreadName) {
    return newFixedPool(parallelism, Integer.MAX_VALUE, name, includeThreadName);
  }

  public static ExecutorService newFixedPool(int parallelism, int queueSize, String name, boolean includeThreadName) {
    ThreadFactory factory = defaultThreadFactory(name, includeThreadName);
    return new ThreadPoolExecutor(parallelism, parallelism, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(queueSize), factory);
  }

  public static void async(ForkJoinPool workers, Runnable runnable, Function errorMessage) {
    async(workers, runnable, null, errorMessage);
  }

  public static void async(
      ForkJoinPool workers, Runnable runnable, Supplier successMessage, Function errorMessage) {
    ForkJoinTask task = workers.submit(runnable);
    try {
      task.get();
      if (successMessage != null) {
        LOG.info("{}", successMessage.get());
      }
    } catch (InterruptedException | ExecutionException e) {
      LOG.error(errorMessage.apply(e), e);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy