1.使用 top 命令定位那个进程使用 CUP 最多.
PID COMMAND %CPU TIME #TH #WQ #PORT MEM PURG CMPRS PGRP
4469 java 0.1 00:00.23 18 1 74 14M+ 0B 0B 2268
4468 java 0.0 00:02.38 20 1 78 110M 0B 0B 2268
2.查看 4469 进行的线程
3.使用 jstack 4469 > /Users/xx/stack.log
Full thread dump Java HotSpot™ 64-Bit Server VM (25.144-b01 mixed mode):
“Attach Listener” #10 daemon prio=9 os_prio=31 tid=0x00007ff2609d3800 nid=0x1107 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
“Service Thread” #9 daemon prio=9 os_prio=31 tid=0x00007ff25f894800 nid=0x4b03 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
“C1 CompilerThread2” #8 daemon prio=9 os_prio=31 tid=0x00007ff260037800 nid=0x4903 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
“C2 CompilerThread1” #7 daemon prio=9 os_prio=31 tid=0x00007ff260037000 nid=0x4703 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
“C2 CompilerThread0” #6 daemon prio=9 os_prio=31 tid=0x00007ff26001e000 nid=0x4503 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
“Monitor Ctrl-Break” #5 daemon prio=5 os_prio=31 tid=0x00007ff25f891800 nid=0x4303 runnable [0x00007000010cb000]
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 sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
- locked <0x000000074000c560> (a java.io.InputStreamReader)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
- locked <0x000000074000c560> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64)
“Signal Dispatcher” #4 daemon prio=9 os_prio=31 tid=0x00007ff26102b000 nid=0x4103 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
“Finalizer” #3 daemon prio=8 os_prio=31 tid=0x00007ff260822000 nid=0x3103 in Object.wait() [0x0000700000ec5000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007400069d8> (a java.lang.ref.ReferenceQueue
L
o
c
k
)
a
t
j
a
v
a
.
l
a
n
g
.
r
e
f
.
R
e
f
e
r
e
n
c
e
Q
u
e
u
e
.
r
e
m
o
v
e
(
R
e
f
e
r
e
n
c
e
Q
u
e
u
e
.
j
a
v
a
:
143
)
−
l
o
c
k
e
d
<
0
x
00000007400069
d
8
>
(
a
j
a
v
a
.
l
a
n
g
.
r
e
f
.
R
e
f
e
r
e
n
c
e
Q
u
e
u
e
Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143) - locked <0x00000007400069d8> (a java.lang.ref.ReferenceQueue
Lock)atjava.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)−locked<0x00000007400069d8>(ajava.lang.ref.ReferenceQueueLock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
“Reference Handler” #2 daemon prio=10 os_prio=31 tid=0x00007ff26081f800 nid=0x2f03 in Object.wait() [0x0000700000dc2000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x000000074000a2f0> (a java.lang.ref.Reference
L
o
c
k
)
a
t
j
a
v
a
.
l
a
n
g
.
O
b
j
e
c
t
.
w
a
i
t
(
O
b
j
e
c
t
.
j
a
v
a
:
502
)
a
t
j
a
v
a
.
l
a
n
g
.
r
e
f
.
R
e
f
e
r
e
n
c
e
.
t
r
y
H
a
n
d
l
e
P
e
n
d
i
n
g
(
R
e
f
e
r
e
n
c
e
.
j
a
v
a
:
191
)
−
l
o
c
k
e
d
<
0
x
000000074000
a
2
f
0
>
(
a
j
a
v
a
.
l
a
n
g
.
r
e
f
.
R
e
f
e
r
e
n
c
e
Lock) at java.lang.Object.wait(Object.java:502) at java.lang.ref.Reference.tryHandlePending(Reference.java:191) - locked <0x000000074000a2f0> (a java.lang.ref.Reference
Lock)atjava.lang.Object.wait(Object.java:502)atjava.lang.ref.Reference.tryHandlePending(Reference.java:191)−locked<0x000000074000a2f0>(ajava.lang.ref.ReferenceLock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
“main” #1 prio=5 os_prio=31 tid=0x00007ff261000000 nid=0x1c03 runnable [0x00007000007b0000]
java.lang.Thread.State: RUNNABLE
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:326)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
- locked <0x0000000740009298> (a java.io.BufferedOutputStream)
at java.io.PrintStream.write(PrintStream.java:482)
- locked <0x00000007400085f8> (a java.io.PrintStream)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:104)
- locked <0x00000007400085b0> (a java.io.OutputStreamWriter)
at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:185)
at java.io.PrintStream.newLine(PrintStream.java:546)
- eliminated <0x00000007400085f8> (a java.io.PrintStream)
at java.io.PrintStream.println(PrintStream.java:807)
- locked <0x00000007400085f8> (a java.io.PrintStream)
at day11.MainClass.main(MainClass.java:6)
“VM Thread” os_prio=31 tid=0x00007ff26100e000 nid=0x2d03 runnable
“GC task thread#0 (ParallelGC)” os_prio=31 tid=0x00007ff25f808800 nid=0x2503 runnable
“GC task thread#1 (ParallelGC)” os_prio=31 tid=0x00007ff25f80f000 nid=0x2703 runnable
“GC task thread#2 (ParallelGC)” os_prio=31 tid=0x00007ff261001000 nid=0x2903 runnable
“GC task thread#3 (ParallelGC)” os_prio=31 tid=0x00007ff25f80f800 nid=0x2b03 runnable
“VM Periodic Task Thread” os_prio=31 tid=0x00007ff25f895000 nid=0x4d03 waiting on condition
JNI global references: 25
用线程 id(转换为 16进制数),去日志文件中匹配 nid 来定位那段代码出现问题.