Code:
package me.konloch.kontainer.api.threading;
import java.util.ArrayList;
/**
* A simple thread manager class
*
* @author Konloch (http://konloch.me | http://the.bytecode.club)
*
* TODO:
* Make addThread return an int for the thread id, then make a basic tracking system
* ^ use some form of map to do this (hashmap?)
*
*/
public class ThreadManager {
private ArrayList<Thread> threadPool;
/**
* Create a new instance of the thread manager
*/
public ThreadManager() {
threadPool = new ArrayList<Thread>();
}
/**
* Adds a thread to the thread pool
* @param t the thread
*/
public void addThread(Thread t) {
threadPool.add(t);
}
/**
* Removes the thread object from the pool
* @param t the cached thread object
*/
public void removeThread(Thread t) {
threadPool.remove(t);
}
/**
* Starts all non-active threads
*/
public void startThreads() {
for(Thread t : threadPool)
if(!t.isAlive())
t.start();
}
/**
* Attempts to halt all the active threads
*/
@SuppressWarnings("deprecation")
public void stopThreads() {
for(Thread t : threadPool)
t.stop(); //< incase the stop function is overrided so they can handle an external stop.
}
/**
* If threads are running it'll return true;
* @return if any threads are running
*/
public boolean areThreadsRunning() {
for(Thread t : threadPool)
if(t.isAlive())
return true;
return false;
}
}
Feel free to do what you want with it, read the TODO list for improvements.