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?
The default configuration of core pool size is 1, max pool size and queue capacity as 2147483647. This is roughly equivalent to Executors.
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.
Thread pool type is fixed with a size of 1 , queue size of 16 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With