Java Themes: Find Out Which Threads Are Still Running

For debugging purposes, I want to find out which threads of my program are still running. There seems to be one or more threads that were not accidentally interrupted. Some good print format would be a bonus.

+3
source share
5 answers

jVisualVM is your friend for this kind of debugging. It is located in the / bin directory of your JDK installation. Shows all threads as a graphical representation and allows you to expand what they do. The button Thread Dumpprints all of its current stack traces so you can see that something is stuck somewhere in your user code.

+3

"jps", java- jstack .

jstack doc

+5

, - ( JDK 1.5 ) :

Map<Thread, StackTraceElement[]> stack = Thread.getAllStackTraces();
for (Map.Entry<Thread, StackTraceElement[]> entry : stack.entrySet()) {
    System.out.println("Thread named '" + 
                       entry.getKey().getName() + 
                       "' is alive");
}
+3
+1

", , ". , . Thread.interrupt() runnable. , , (, Thread.sleep java.nio), InterruptedException - (, ClosedByInterruptException nio). runnables Thread.currentThread(). IsInterrupted() .

0

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


All Articles