The Bytecode Club

Full Version: Basic/Tiny Thread Manager
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
This inspired me to write this...

https://gist.github.com/anonymous/1e8de4e6a5d112cc8815 Enjoy~
(03-01-2015, 01:18 PM)Septron Wrote: [ -> ]This inspired me to write this...

https://gist.github.com/anonymous/1e8de4e6a5d112cc8815 Enjoy~

Awesome job!