Search code examples
javamultithreadingthreadpoolexecutor

Finding the Names of active threads inside a threadpool : ExecutorService in java


So I am running an executor service and I would like to know the Names or the threadIDs of all the currently active/idle threads.

ExecutorService service = Executors.newCachedThreadPool(ThreadFactory threadFactory)

I do not need to know the count, but the actual names/IDs of all the active threads in my executor service. I need to identify the threads in any manner because I plan on implementing my own ThreadFactory with an appropriate naming convention.

For example, if my active threads are T0,T1,T3, my threadfactory would name the next thread as T2. But I can't find a way to get information about the active Threads. How can I do that?

PS : Any other methods would also be appreciated. For example, lets say I am fine with having threads with names from T0 to T50. I just want my current threadfactory to assign any name from T0 to T50 such that a thread with the same name is not currently active or idle.


Solution

  • I was so free to create you a sample, of something I would've done. I couldn't really test it though:

    public class CachingThreadFactory implements ThreadFactory{
        // amount of active threads at max
        private static final int THREAD_POOL_MAX_SIZE = 8;
    
        // interval in milliseconds of the clean up task
        private static final int CLEAN_UP_INTERVAL = 2000;
    
        // the actual cache
        private final Thread[] cachedThreads = new Thread[THREAD_POOL_MAX_SIZE];
    
        // clean up task definition
        {
            new Timer().scheduleAtFixedRate(new CleanUpTask(), 0, CLEAN_UP_INTERVAL);
        }
    
        @Override
        public synchronized Thread newThread(Runnable r){
            for(int i = 0; i < cachedThreads.length; i++){
                if(cachedThreads[i] == null){
                    return cachedThreads[i] = new Thread(r, "T" + i);
                }
            }
            return null;
        }
    
        private final class CleanUpTask extends TimerTask{
            @Override
            public void run(){
                synchronized(CachingThreadFactory.this){
                    for(int i = 0; i < cachedThreads.length; i++){
                        final Thread thread = cachedThreads[i];
                        if(thread != null && !thread.isAlive()){
                            cachedThreads[i] = null; // unset
                        }
                    }
                }
            }
        }
    }
    

    This Factory caches every Thread it creates in an array. Then it runs a cleanUpTask asynchronly which checks if the threads in the array (if any) are still alive. If not they are removed.

    The newThread method iterates through the cache, to find an index which is not yet taken, and then uses that index to create the name of that Thread. If no place is free it just returns null.

    This class is probably thread safe. But I haven't really tested it. The synchronized-statements should prevent the interference between the cleanUp-Task and the newThread method. But any other action may disturb the whole thing.