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
#2
This inspired me to write this...

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

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

Awesome job!
Reply
 


Forum Jump:


Users browsing this thread: 1 Guest(s)




About The Bytecode Club

We're a community 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.