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

com.softicar.platform.common.core.thread.RunnableThread 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.thread;

import com.softicar.platform.common.core.thread.collection.ThreadKiller;
import com.softicar.platform.common.core.threading.Threads;
import java.util.Objects;
import java.util.function.Function;

/**
 * Combines a {@link Runnable} and a {@link Thread} object.
 *
 * @author Alexander Schmidt
 * @author Oliver Richers
 */
public class RunnableThread implements IRunnableThread {

	private final R runnable;
	private final Thread thread;

	/**
	 * Constructs a new instance for the given {@link Runnable}.
	 *
	 * @param runnable
	 *            the {@link Runnable} (never null)
	 */
	public RunnableThread(R runnable) {

		this(runnable, Thread::new);
	}

	/**
	 * Constructs a new instance for the given {@link Runnable}, using the given
	 * {@link Thread} factory.
	 *
	 * @param runnable
	 *            the {@link Runnable} (never null)
	 * @param threadFactory
	 *            a factory to create a {@link Thread} for the {@link Runnable}
	 *            (never null)
	 */
	public RunnableThread(R runnable, Function threadFactory) {

		this.runnable = Objects.requireNonNull(runnable);
		this.thread = Objects.requireNonNull(threadFactory.apply(runnable));
	}

	@Override
	public R getRunnable() {

		return runnable;
	}

	@Override
	public void start() {

		thread.start();
	}

	@Override
	public void interrupt() {

		thread.interrupt();
	}

	@Override
	public boolean kill() {

		return new ThreadKiller<>(thread).killAll();
	}

	@Override
	public boolean isTerminated() {

		return Threads.isTerminated(thread);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy