测试类:
import java.util.Date;
public class SimpleJavaApp{
public static void main(String args[]) throws Exception{
while(true){
System.out.println(new Date());
Thread.sleep(1000);
}
}
}
一共有6中方法监视线程状态,下面分别介绍:
jstack (命令行,平台无关)
JMC (命令行,平台无关)
jvisualvm (图形化,平台无关)
kill -3 PID (图形化,linux)
CTRL+\ (命令行,linux)
CTRL+Break (命令行,windows)
jstack
命令行:jstack -l PID(平台无关),PID为Java程序的进程ID。Java程序的进程ID可以使用JDK的jps命令等。
jacob@jacob-pc:~$ jstack -l 16771
2018-09-27 17:24:55
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.181-b13 mixed mode):
"Attach Listener" #9 daemon prio=9 os_prio=0 tid=0x00007fcc64001000 nid=0x41d6 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"Service Thread" #8 daemon prio=9 os_prio=0 tid=0x00007fcca00e3800 nid=0x4191 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"C1 CompilerThread2" #7 daemon prio=9 os_prio=0 tid=0x00007fcca00be000 nid=0x4190 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"C2 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007fcca00bc800 nid=0x418f waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007fcca00b9800 nid=0x418e waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007fcca00b8000 nid=0x418d runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007fcca0085000 nid=0x418c in Object.wait() [0x00007fcc8846b000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000ec108ed0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0x00000000ec108ed0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)
Locked ownable synchronizers:
- None
"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007fcca0082800 nid=0x418b in Object.wait() [0x00007fcc8856c000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000ec106bf8> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x00000000ec106bf8> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
Locked ownable synchronizers:
- None
"main" #1 prio=5 os_prio=0 tid=0x00007fcca000b000 nid=0x4184 waiting on condition [0x00007fcca6bd2000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at SimpleJavaApp.main(SimpleJavaApp.java:6)
Locked ownable synchronizers:
- None
"VM Thread" os_prio=0 tid=0x00007fcca0079000 nid=0x418a runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007fcca0020800 nid=0x4185 runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007fcca0022800 nid=0x4186 runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007fcca0024000 nid=0x4187 runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007fcca0026000 nid=0x4188 runnable
"VM Periodic Task Thread" os_prio=0 tid=0x00007fcca00e8800 nid=0x4192 waiting on condition
JNI global references: 5
JMC
JMC是Oracle JDK1.8自带的一个工具,其可执行文件是:JDK主目录/bin/jmc。
jvisualvm
图形化界面(平台无关),jvisualvm使Oracle JDK自带的一个工具,其可执行文件是:JDK主目录/bin/jvisualvm
kill -3 PID和“CTRL+\”
命令行(仅限于linux),这两个命令都可以,效果一样的,但是CTRL+\需要在当前运行的控制台中执行。
2018-09-27 18:03:10
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.181-b13 mixed mode):
"RMI TCP Connection(2)-127.0.0.1" #15 daemon prio=9 os_prio=0 tid=0x00007fcc50003000 nid=0x4774 runnable [0x00007fcc827f5000]
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 <0x00000000c4237950> (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/821511329.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)
"JMX server connection timeout 14" #14 daemon prio=9 os_prio=0 tid=0x00007fcc44015000 nid=0x4771 in Object.wait() [0x00007fcc828f7000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at com.sun.jmx.remote.internal.ServerCommunicatorAdmin$Timeout.run(ServerCommunicatorAdmin.java:168)
- locked <0x00000000c423a698> (a [I)
at java.lang.Thread.run(Thread.java:748)
"RMI Scheduler(0)" #13 daemon prio=9 os_prio=0 tid=0x00007fcc44012800 nid=0x476f waiting on condition [0x00007fcc829f8000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000c423a9b8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
"RMI TCP Accept-0" #11 daemon prio=9 os_prio=0 tid=0x00007fcc4c13d000 nid=0x476b runnable [0x00007fcc82bfa000]
java.lang.Thread.State: RUNNABLE
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:409)
at java.net.ServerSocket.implAccept(ServerSocket.java:545)
at java.net.ServerSocket.accept(ServerSocket.java:513)
at sun.management.jmxremote.LocalRMIServerSocketFactory$1.accept(LocalRMIServerSocketFactory.java:52)
at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.executeAcceptLoop(TCPTransport.java:405)
at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.run(TCPTransport.java:377)
at java.lang.Thread.run(Thread.java:748)
"Attach Listener" #9 daemon prio=9 os_prio=0 tid=0x00007fcc64001000 nid=0x41d6 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Service Thread" #8 daemon prio=9 os_prio=0 tid=0x00007fcca00e3800 nid=0x4191 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread2" #7 daemon prio=9 os_prio=0 tid=0x00007fcca00be000 nid=0x4190 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007fcca00bc800 nid=0x418f waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007fcca00b9800 nid=0x418e waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007fcca00b8000 nid=0x418d waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007fcca0085000 nid=0x418c in Object.wait() [0x00007fcc8846b000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0x00000000c4250e90> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)
"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007fcca0082800 nid=0x418b in Object.wait() [0x00007fcc8856c000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x00000000c42510c0> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"main" #1 prio=5 os_prio=0 tid=0x00007fcca000b000 nid=0x4184 waiting on condition [0x00007fcca6bd2000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at SimpleJavaApp.main(SimpleJavaApp.java:6)
"VM Thread" os_prio=0 tid=0x00007fcca0079000 nid=0x418a runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007fcca0020800 nid=0x4185 runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007fcca0022800 nid=0x4186 runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007fcca0024000 nid=0x4187 runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007fcca0026000 nid=0x4188 runnable
"VM Periodic Task Thread" os_prio=0 tid=0x00007fcca00e8800 nid=0x4192 waiting on condition
JNI global references: 204
Heap
PSYoungGen total 17920K, used 15032K [0x00000000ec100000, 0x00000000ed500000, 0x0000000100000000)
eden space 15360K, 97% used [0x00000000ec100000,0x00000000ecf963c8,0x00000000ed000000)
from space 2560K, 3% used [0x00000000ed280000,0x00000000ed298000,0x00000000ed500000)
to space 2560K, 0% used [0x00000000ed000000,0x00000000ed000000,0x00000000ed280000)
ParOldGen total 16896K, used 1484K [0x00000000c4200000, 0x00000000c5280000, 0x00000000ec100000)
object space 16896K, 8% used [0x00000000c4200000,0x00000000c43732e0,0x00000000c5280000)
Metaspace used 8817K, capacity 9122K, committed 9344K, reserved 1056768K
class space used 999K, capacity 1079K, committed 1152K, reserved 1048576K
CTRL+Break
命令行(仅限windowns使用),在启动Java程序的命令行提示窗口中执行。
示例:略