线程的监视

测试类:

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。

JMC

jvisualvm
图形化界面(平台无关),jvisualvm使Oracle JDK自带的一个工具,其可执行文件是:JDK主目录/bin/jvisualvm

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程序的命令行提示窗口中执行。
示例:略

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,968评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,601评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,220评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,416评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,425评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,144评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,432评论 3 401
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,088评论 0 261
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,586评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,028评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,137评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,783评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,343评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,333评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,559评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,595评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,901评论 2 345

推荐阅读更多精彩内容