Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 855
static-max Avatar asked Aug 16 '13 15:08

static-max


People also ask

What is Queuecapacity in ThreadPoolTaskExecutor?

The default configuration of core pool size is 1, max pool size and queue capacity as 2147483647. This is roughly equivalent to Executors.

How do you clear ThreadPoolExecutor?

Java ThreadPoolExecutor remove() MethodThe remove() method of ThreadPoolExecutor class removes this task from the executor's internal queue if it is present, thus causing it not to be run if it has not already started.

What is thread pool queue size?

Thread pool type is fixed with a size of 1 , queue size of 16 .

What is core pool size in ThreadPoolTaskExecutor?

The default configuration is a core pool size of 1, with unlimited max pool size and unlimited queue capacity. This is roughly equivalent to Executors. newSingleThreadExecutor() , sharing a single thread for all tasks.


1 Answers

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.

like image 149
static-max Avatar answered Nov 15 '22 01:11

static-max