一、命令行工具jstack

用于查看线程状态

  • 例子

    1. public class ThreadState {
    2. public static void main(String[] args) {
    3. new Thread(new TimeWaiting(), "TimeWaitingThread").start();
    4. new Thread(new Waiting(), "WaitingThread").start();
    5. // 使用两个Blocked线程,一个获取锁成功,另一个被阻塞
    6. new Thread(new Blocked(), "BlockedThread-1").start();
    7. new Thread(new Blocked(), "BlockedThread-2").start();
    8. }
    9. // 该线程不断地进行睡眠
    10. static class TimeWaiting implements Runnable {
    11. @Override
    12. public void run() {
    13. while (true) {
    14. try {
    15. TimeUnit.SECONDS.sleep(100);
    16. } catch (InterruptedException e) {
    17. e.printStackTrace();
    18. }
    19. }
    20. }
    21. }
    22. // 该线程在Waiting.class实例上等待
    23. static class Waiting implements Runnable {
    24. @Override
    25. public void run() {
    26. while (true) {
    27. synchronized (Waiting.class) {
    28. try {
    29. Waiting.class.wait();
    30. } catch (InterruptedException e) {
    31. e.printStackTrace();
    32. }
    33. }
    34. }
    35. }
    36. }
    37. // 该线程在Blocked.class实例上加锁后,不会释放该锁
    38. static class Blocked implements Runnable {
    39. public void run() {
    40. synchronized (Blocked.class) {
    41. while (true) {
    42. try {
    43. TimeUnit.SECONDS.sleep(100);
    44. } catch (InterruptedException e) {
    45. e.printStackTrace();
    46. }
    47. }
    48. }
    49. }
    50. }
    51. }
  • 启动以上程序后,控制台上输入jps 查看进程

    1. $ jps
    2. 7012 Main
    3. 32037 Jps
    4. 26870 Main
    5. 31862 Launcher
    6. 31863 ThreadState
    7. 29629 KotlinCompileDaemon
  • 查看 ThreadState 的状态 : jstack 进程id

  1. jstack 31863
  1. //...省略
  2. //BlockedThread-2 阻塞状态: BLOCKED
  3. "BlockedThread-2" #13 prio=5 os_prio=0 tid=0x00007f0dd8214800 nid=0x7c98 waiting for monitor entry [0x00007f0dc1600000]
  4. java.lang.Thread.State: BLOCKED (on object monitor)
  5. at concurrency.ThreadState$Blocked.run(ThreadState.java:54)
  6. - waiting to lock <0x00000000d93089b0> (a java.lang.Class for concurrency.ThreadState$Blocked)
  7. at java.lang.Thread.run(Thread.java:748)
  8. // BlockedThread-1 获取了锁,执行sleep()方法,进入超时等待状态: TIMED_WAITING
  9. "BlockedThread-1" #12 prio=5 os_prio=0 tid=0x00007f0dd8213000 nid=0x7c97 waiting on condition [0x00007f0dc1701000]
  10. java.lang.Thread.State: TIMED_WAITING (sleeping)
  11. at java.lang.Thread.sleep(Native Method)
  12. at concurrency.ThreadState$Blocked.run(ThreadState.java:54)
  13. - locked <0x00000000d93089b0> (a java.lang.Class for concurrency.ThreadState$Blocked)
  14. at java.lang.Thread.run(Thread.java:748)
  15. //WaitingThread 等待状态: WAITING
  16. "WaitingThread" #11 prio=5 os_prio=0 tid=0x00007f0dd8211000 nid=0x7c96 in Object.wait() [0x00007f0dc1802000]
  17. java.lang.Thread.State: WAITING (on object monitor)
  18. at java.lang.Object.wait(Native Method)
  19. - waiting on <0x00000000d9308dc8> (a java.lang.Class for concurrency.ThreadState$Waiting)
  20. at java.lang.Object.wait(Object.java:502)
  21. at concurrency.ThreadState$Waiting.run(ThreadState.java:39)
  22. - locked <0x00000000d9308dc8> (a java.lang.Class for concurrency.ThreadState$Waiting)
  23. at java.lang.Thread.run(Thread.java:748)
  24. // TimeWaitingThread 超时等待状态:TIMED_WAITING
  25. "TimeWaitingThread" #10 prio=5 os_prio=0 tid=0x00007f0dd8210000 nid=0x7c95 waiting on condition [0x00007f0dc1903000]
  26. java.lang.Thread.State: TIMED_WAITING (sleeping)
  27. at java.lang.Thread.sleep(Native Method)
  28. at concurrency.ThreadState$TimeWaiting.run(ThreadState.java:24)
  29. at java.lang.Thread.run(Thread.java:748)
  30. //省略...

二、vmstat 查看线程切换次数

参考
https://wiki.openjdk.java.net/display/CodeTools