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

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

import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * Represents a {@link Collection} of {@link Thread} objects.
 *
 * @author Oliver Richers
 */
public class ThreadCollection extends AbstractCollection {

	private final Set threads;

	public ThreadCollection() {

		this.threads = new HashSet<>();
	}

	public ThreadCollection(T thread) {

		this(Collections.singleton(thread));
	}

	public ThreadCollection(Collection threads) {

		this.threads = new HashSet<>(threads);
	}

	public ThreadCollection(ThreadCollection threads) {

		this.threads = new HashSet<>(threads.threads);
	}

	@Override
	public boolean add(T thread) {

		return threads.add(thread);
	}

	@Override
	public Iterator iterator() {

		return threads.iterator();
	}

	@Override
	public int size() {

		return threads.size();
	}

	public void startAll() {

		threads.stream().forEach(Thread::start);
	}

	public void interruptAll() {

		threads.stream().forEach(Thread::interrupt);
	}

	public boolean joinAll(long timeoutMillis) {

		return new ThreadJoiner<>(this).joinAll(timeoutMillis);
	}

	public boolean killAll() {

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

	public boolean runAll(long timeoutMillis) {

		return new ThreadRunner<>(this).runAll(timeoutMillis);
	}

	public void removeFinishedThreads() {

		for (Iterator iterator = threads.iterator(); iterator.hasNext();) {
			Thread thread = iterator.next();
			if (!thread.isAlive()) {
				iterator.remove();
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy