youyichannel

志于道,据于德,依于仁,游于艺!

0%

JUC初探-03

进程状态转化 —— 解读 Stack Log

通过 Stack Log 查看线程状态作用:排查线程异常状态、正常线程状态的统计、评估线程的上下文切换等等。

使用jps + jstack命令来查看Stack Log

示例代码:

public class ReadStackLog {
public static void main(String[] args) {
new Thread(new TimeWaiting(), "TimeWaitingThread").start();
new Thread(new Waiting(), "WaitingThread").start();
// 使用两个Blocked线程,一个获取锁成功,另一个被阻塞
new Thread(new Blocked(), "BlockedThread-1").start();
new Thread(new Blocked(), "BlockedThread-2").start();
}
}

/**
* 该线程不断进行睡眠
*/
class TimeWaiting implements Runnable {

@Override
public void run() {
while (true) {
try {
Thread.sleep(1000_000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}

/**
* 该线程竞争Waiting.class,在Waiting.class实例上等待
*/
class Waiting implements Runnable {

@Override
public void run() {
while (true) {
synchronized (Waiting.class) {
try {
Waiting.class.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
}

/**
* 该线程在Blocked.class实例上加锁之后,不会释放该锁
*/
class Blocked implements Runnable {

@Override
public void run() {
synchronized (Blocked.class) {
while (true) {
try {
Thread.sleep(1000_000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
}

相关的Stack Log

"Attach Listener" #16 daemon prio=9 os_prio=31 tid=0x000000014580c000 nid=0x4207 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"DestroyJavaVM" #15 prio=5 os_prio=31 tid=0x0000000124012000 nid=0x1403 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"BlockedThread-2" #14 prio=5 os_prio=31 tid=0x0000000146045000 nid=0x7a03 waiting for monitor entry [0x000000017214e000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.juzi.juc.Blocked.run(ReadStackLog.java:62)
- waiting to lock <0x000000076aeda670> (a java.lang.Class for com.juzi.juc.Blocked)
at java.lang.Thread.run(Thread.java:750)

"BlockedThread-1" #13 prio=5 os_prio=31 tid=0x0000000146044000 nid=0x7b03 waiting on condition [0x0000000171f42000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at com.juzi.juc.Blocked.run(ReadStackLog.java:62)
- locked <0x000000076aeda670> (a java.lang.Class for com.juzi.juc.Blocked)
at java.lang.Thread.run(Thread.java:750)

"WaitingThread" #12 prio=5 os_prio=31 tid=0x0000000146043000 nid=0x5903 in Object.wait() [0x0000000171d36000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x000000076aed7d50> (a java.lang.Class for com.juzi.juc.Waiting)
at java.lang.Object.wait(Object.java:502)
at com.juzi.juc.Waiting.run(ReadStackLog.java:43)
- locked <0x000000076aed7d50> (a java.lang.Class for com.juzi.juc.Waiting)
at java.lang.Thread.run(Thread.java:750)

"TimeWaitingThread" #11 prio=5 os_prio=31 tid=0x0000000146042000 nid=0x7c03 waiting on condition [0x0000000171b2a000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at com.juzi.juc.TimeWaiting.run(ReadStackLog.java:25)
at java.lang.Thread.run(Thread.java:750)


"Monitor Ctrl-Break" #5 daemon prio=5 os_prio=31 tid=0x0000000123809000 nid=0x4603 runnable [0x0000000170cd6000]
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 <0x000000076aca7168> (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 <0x000000076aca7168> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:53)

"Signal Dispatcher" #4 daemon prio=9 os_prio=31 tid=0x000000014500f000 nid=0x4f03 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"Finalizer" #3 daemon prio=8 os_prio=31 tid=0x0000000143852800 nid=0x3303 in Object.wait() [0x00000001707a6000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x000000076ab09528> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0x000000076ab09528> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:188)

"Reference Handler" #2 daemon prio=10 os_prio=31 tid=0x0000000143850000 nid=0x3603 in Object.wait() [0x000000017059a000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x000000076ab070a8> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x000000076ab070a8> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

几个需要注意的点:

1)"BlockedThread-2"状态为BLOCKED,我们能从中发现死锁情况,根据日志- waiting to lock <0x000000076aeda670> (a java.lang.Class for com.juzi.juc.Blocked),还可以定位到具体的类com.juzi.juc.Blocked

2)"BlockedThread-1"状态为TIMED_WAITING,这个和我们的代码意图一直,除此之外,根据日志,我们还可以发现- locked <0x000000076aeda670> (a java.lang.Class for com.juzi.juc.Blocked),该线程已经获取了锁

3)"WaitingThread"的日志可以看出它调用了Object.wait()方法,这意味着,如果该线程不被notify的话,会一直等待下去

4)"Finalizer"线程的状态为WAITING,而且是调用了Object.wait()方法的,之前提过,只有进行垃圾收集的时候,该线程才会被"Signal Dispatcher"线程进行notify

5)"Reference Handler"的状态为WAITING,该线程是引用处理线程,目前引用已经全部处理完成,因此该线程陷入等待,只有当我们加载新的类的时候,该线程才会又进入工作状态,即被notify

资料:https://www.bilibili.com/video/BV13u4y1w7YL/ 强推 河北王校长