List of all started / set topics in ThreadPoolTaskExecutor

I am using ThreadPoolTaskExecutor in Spring to plan my tasks.

Is there a way to get a list or something from each running and queued thread of this executor / task pool?

+6
source share
3 answers

Maybe not very elegant, but I can get all the threads from the famous 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 supported his approval to get the lines in line.

+2
source

It looks like you can get the base ThreadPoolExecutor by calling getThreadPoolExecutor .

This will allow you to access the queue at least.

0
source

One possible way is to use Reflection or Spring provided that ReflectionUtils or ReflectionTestUtils ..

I used ReflectionTestUtils in my Junit to test the script when one or more sleeping threads are interrupted.

Code snippet:

 Collection<Object> workers = (Collection<Object>) ReflectionTestUtils.getField(responseHandlerTaskExecutor.getThreadPoolExecutor(), "workers"); for(Object worker : workers){ Thread workerThread = (Thread)ReflectionTestUtils.getField(worker, "thread"); workerThread.interrupt(); } 
0
source

Source: https://habr.com/ru/post/951896/


All Articles