Search code examples
javathreadpooljava.util.concurrentthreadpoolexecutor

List all running/queued threads in ThreadPoolTaskExecutor


I use a ThreadPoolTaskExecutor in Spring to schedule my tasks.

Is there a way to get a list or something of every running and queued thread of that task executor/pool?


Solution

  • Maybe not very elegant, but this way I can get all threads from a known Executor (using the startsWith() prefix).

    Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
    for (Thread thread : threadSet) {
      if (thread.getName().startsWith("MyExecutor")) {
      System.out.println(thread.getName() + " " + thread.getState());
        for (StackTraceElement s : thread.getStackTrace()) {
          System.out.println(s);
        }
      }
    }
    

    Thanks to Surveon for his hint, I upvoted for his approch to get the queued threads.