Java thread monitoring jvisualvm and jstack

Five states of threads

  • New: new

  • Run: runnable

  • Waiting: waiting (waiting indefinitely), timed waiting (waiting within a time limit)

  • blocked: blocked

  • End: terminated

    Thread conversion relationship

image

Two monitoring methods for threads

1. Graphical interface method: jvisualvm

  • Before monitoring, first add monitoring parameters to the jvm. In the catalina.sh file in the bin directory of tomcat, add the second line:
#!/bin/sh
JAVA_OPTS="-Dcom.sun.management.jmxremote.port=8080 (Open the monitoring port as desired, as long as it is not occupied) -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote .authenticate=false -Djava.rmi.server.hostname=192.168.1.30 (IP of Tomcat machine)"
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with

1-Change the port, the monitored port can be unoccupied
2-Change hostname to local ip
3-Natural line break, no manual carriage return required

If you want to monitor multiple tomcats, you need to add this line of parameters under each tomcat, and the port numbers should not be repeated.

  • Save and restart tomcat

Check whether the port number 8080 is occupied: netstat -an|grep 8080
Or view the usage details of 8080: lsof -i:8080

When ready, start monitoring tomcat threads now

1. Win system console input jvisualvm to start JAVA Visualvm

image

2. My tomcat below is installed on this machine

image

2. Command line mode: jstack pid (process number). –Stack printing information

  • This command is generally executed on the Tomcat machine (executed in the production environment) and can only obtain the instantaneous status.

step

1. First query the process number of tomcat: ps -ef|grep tomcatimage
2. Execution name: jstack 8743

PS D:\MyData\Desktop> jstack 8743
2021-09-15 11:17:06
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.281-b09 mixed mode):

"RMI TCP Connection(8)-10.73.46.35" #76 daemon prio=5 os_prio=0 tid=0x0000016799055800 nid=0x8f50 runnable [0x000000189dcfe000]
   java.lang.Thread.State: RUNNABLE
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
        at java.net.SocketInputStream.read(SocketInputStream.java:171)
        at java.net.SocketInputStream.read(SocketInputStream.java:141)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
        - locked <0x00000006c4683a60> (a java.io.BufferedInputStream)
        at java.io.FilterInputStream.read(FilterInputStream.java:83)
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:555)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda$3/819083873.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)

"http-nio-8080-exec-24" #71 daemon prio=5 os_prio=0 tid=0x000001679905a000 nid=0x61e8 waiting for monitor entry [0x00000018a10fb000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at java.io.PrintStream.println(PrintStream.java:806)
        - waiting to lock <0x00000006c2249ec8> (a java.io.PrintStream)
        at org.apache.tomcat.util.log.SystemLogHandler.println(SystemLogHandler.java:267)
        at cn.itcast.oa.util.HqlHelper.buildPageBeanForStruts2(HqlHelper.java:151)
        at cn.itcast.oa.view.action.UserAction.list(UserAction.java:33)
...
...

3. Since this command will directly display the content on the screen and is difficult to view, it is best to redirect the output: jstack 8743 > a.log

  • The data returned by the command can be generated into an a.log file in the current directory for easy viewingimage

Java thread stack information analysis

  • Thread name: “http-nio-8080-exec-24”
  • Priority: prio=10, the default is 5, the larger the value, the higher the priority
  • tid=0x000001679905a000: jvm thread id, the unique identifier of the jvm internal thread
  • nid=0x61e8: Corresponds to the system thread ID (NativeThread ID), and corresponds to the thread pid viewed by the top command, but one is in decimal and the other is in hexadecimal. (You can view all thread information of the process through the command: top -H -p pid)
  • Thread state: java.lang.Thread.State: BLOCKED (on object monitor)
  • The method the thread is executing at this time, and the call chain: (Looking from bottom to top, the first one is being executed)

at java.io.PrintStream.println(PrintStream.java:806)
waiting to lock <0x00000006c2249ec8> (a java.io.PrintStream)
at org.apache.tomcat.util.log.SystemLogHandler.println(SystemLogHandler.java:267)
at cn.itcast.oa.util.HqlHelper.buildPageBeanForStruts2(HqlHelper.java:151)
at cn.itcast.oa.view.action.UserAction.list(UserAction.java:33)

  • http requests are generally made by Tomcat

3. Jar package monitoring thread

  • Java script to construct various thread states
    Command: java -jar thread-test.jar [1|2|3|4]
    Construct threads in different states
    1: runnable
    2:waiting
    3: timedWaiting
    4: blocked

  • Add monitoring parameters to the Java program java -Djava.rmi.server.hostname=192.168.1.28 -Dcom.sun.management.jmxremote.port=10086 - Dcom.sun.management.jmxremote.ssl=false -Dcom.sun .management.jmxremote.authenticate=false -jar thread-test.jar 1

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 132112 people are learning the system