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

com.softicar.platform.common.core.threading.Threads Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.core.threading;

import java.lang.Thread.State;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.function.Supplier;

/**
 * Some utility methods for {@link Thread}.
 *
 * @author Oliver Richers
 */
public class Threads {

	/**
	 * Joins the given thread.
	 *
	 * @param thread
	 *            the thread to join
	 * @throws InterruptedRuntimeException
	 *             if an {@link InterruptedException} is thrown by
	 *             {@link Thread#join()}.
	 */
	public static void join(Thread thread) {

		try {
			thread.join();
		} catch (InterruptedException exception) {
			throw new InterruptedRuntimeException(exception);
		}
	}

	/**
	 * Joins the given thread with the specified timeout.
	 *
	 * @param thread
	 *            the thread to join
	 * @param millis
	 *            timeout in milliseconds
	 * @throws InterruptedRuntimeException
	 *             if an {@link InterruptedException} is thrown by
	 *             {@link Thread#join()}.
	 */
	public static void join(Thread thread, long millis) {

		try {
			thread.join(millis);
		} catch (InterruptedException exception) {
			throw new InterruptedRuntimeException(exception);
		}
	}

	/**
	 * Runs the given supplier in a child {@link Thread}.
	 *
	 * @param supplier
	 *            the supplier to execute
	 * @return the supplier return value
	 */
	public static  T runInChildThread(Supplier supplier) {

		try {
			return Executors.newSingleThreadExecutor().submit(() -> supplier.get()).get();
		} catch (InterruptedException exception) {
			throw new InterruptedRuntimeException(exception);
		} catch (ExecutionException exception) {
			throw new RuntimeException(exception);
		}
	}

	/**
	 * Determines whether the given {@link Thread} is terminated, as per its
	 * {@link State}.
	 *
	 * @param thread
	 *            the {@link Thread} to check (never null)
	 * @return true if the {@link Thread} is terminated; false
	 *         otherwise
	 */
	public static boolean isTerminated(Thread thread) {

		Objects.requireNonNull(thread);
		return thread.getState() == State.TERMINATED;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy