Java线程监控之 jvisualvm 和 jstack
线程的五种状态
-
新建:new
-
运行:runnable
-
等待:waitting(无限期等待),timed waitting(限期等待)
-
阻塞:blocked
-
结束:terminated
线程转换关系
线程的两种监控方法
1、图形界面的方式:jvisualvm
- 监控之前先对jvm加监控参数,在tomcat的bin目录下,catalina.sh文件中,第二行添加:
#!/bin/sh
JAVA_OPTS="-Dcom.sun.management.jmxremote.port=8080(开启监控的端口,随意,未被占用即可) -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=192.168.1.30(Tomcat机器的IP)"
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
1-改port,监控的端口,未被占用即可
2-改hostname为本机ip
3-自然换行,不需要手动回车如果想要监控和多个tomcat,则需要在每一个tomcat下都添加这行参数,且端口号不重复
- 保存并重启tomcat
查看8080的端口号是否被占用:netstat -an|grep 8080
或者查看8080的使用详情:lsof -i:8080
准备就绪,下面开始监控tomcat的线程
1、win系统控制台输入 jvisualvm 启动JAVA Visualvm
2、下面我的tomcat安装在本机上
2、命令行方式:jstack pid(进程号). --堆栈打印信息
- 该命令一般在Tomcat机器上执行(生产环境上执行),只能获得瞬间的状态
步骤
1、先查询到tomcat的进程号:
ps -ef|grep tomcat
2、执行名称: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、由于此命令会直接把内容展示在屏幕上,不好查看,所以最好重定向输出:
jstack 8743 > a.log
- 就可以将命令返回的数据在当前目录下生成一个a.log文件,方便查看
Java线程堆栈信息分析
- 线程名称:"http-nio-8080-exec-24"
- 优先级: prio=10 ,默认是5,数值越大,优先级越高
- tid=0x000001679905a000:jvm线程id,jvm内部线程的唯一标识
- nid=0x61e8:对应系统线程id(NativeThread ID),和top命令查看的线程pid对应,不过一个是10进制,一个是16进制。(通过命令:top -H -p pid,可以查看该进程的所有线程信息)
- 线程状态: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)
- http请求一般是Tomcat发出请求的
3、jar包监控线程
-
构造各种线程状态的Java脚本
命令:java -jar thread-test.jar [1|2|3|4]
构造不同的状态的线程
1:runnable
2:waitting
3:timedWaitting
4:blocked -
给Java程序添加监控参数
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