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

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





© 2015 - 2024 Weber Informatics LLC | Privacy Policy