com.softicar.platform.common.core.thread.RunnableThread Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of platform-common Show documentation
Show all versions of platform-common Show documentation
The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.
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);
}
}