Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic/Tiny Thread Manager
#1
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.
Reply
 


Messages In This Thread
Basic/Tiny Thread Manager - by Konloch - 10-02-2014, 07:18 AM
RE: Basic/Tiny Thread Manager - by Septron - 03-01-2015, 01:18 PM
RE: Basic/Tiny Thread Manager - by Konloch - 03-01-2015, 02:25 PM

Forum Jump:


Users browsing this thread: 2 Guest(s)

About The Bytecode Club

We're a community forum focused on Reverse Engineering, we try to target Java/Android but we also include other langauges/platforms. We pride ourselves in supporting and free and open sourced applications.

Website