com.softicar.platform.common.core.thread.collection.ThreadRunner 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.collection;
/**
* Runs the given {@link Thread} objects and kills those that take too long.
*
* @author Oliver Richers
*/
public class ThreadRunner {
private final ThreadCollection threads;
public ThreadRunner(Thread thread) {
this.threads = new ThreadCollection<>(thread);
}
public ThreadRunner(ThreadCollection threads) {
this.threads = new ThreadCollection<>(threads);
}
/**
* Starts all threads and joins them.
*
* If one thread runs for more than the specified amount of milliseconds,
* the thread is killed. And this method returns false.
*
* @param timeoutMillis
* maximum time to wait for the threads to finish
* @return true if all threads finished before timeout, false
* if at least one thread was killed because of timeout
*/
public boolean runAll(long timeoutMillis) {
threads.startAll();
threads.joinAll(timeoutMillis);
threads.removeFinishedThreads();
if (threads.isEmpty()) {
return true;
} else {
threads.killAll();
return false;
}
}
}