当前位置:网站首页>Arthas thread command locates thread deadlock

Arthas thread command locates thread deadlock

2022-06-23 07:15:00 qq_ thirty-seven million two hundred and seventy-nine thousand

Scenario simulating deadlock

public class Deadlock {
    private static Object obj1 = new Object();
    private static Object obj2 = new Object();

    public static void main(String[] args) {
        new Thread(() -> {
            System.out.println(" Threads 1 perform ");
            synchronized (obj1) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (obj2) {
                }
            }
        }, "t1").start();
        new Thread(() -> {
            System.out.println(" Threads 2 perform ");
            synchronized (obj2) {
                synchronized (obj1) {
                }
            }
        }, "t2").start();
        System.out.println(" completion of enforcement ");
    }
}

adopt thread Command positioning

1, Use it directly ”thread“ command , Output thread Statistics . among :BLOCKED Indicates the number of threads currently blocked .
[[email protected]]$ thread

Threads Total: 26, NEW: 0, RUNNABLE: 8, BLOCKED: 2, WAITING: 4, TIMED_WAITING: 2, TERMINATED: 0, Internal threads: 10
ID   NAME                          GROUP          PRIORITY  STATE    %CPU      DELTA_TIM TIME      INTERRUPT DAEMON
2    Reference Handler             system         10        WAITING  0.0       0.000     0:0.000   false     true
3    Finalizer                     system         8         WAITING  0.0       0.000     0:0.000   false     true
4    Signal Dispatcher             system         9         RUNNABLE 0.0       0.000     0:0.000   false     true
5    Attach Listener               system         5         RUNNABLE 0.0       0.000     0:0.031   false     true
14   arthas-timer                  system         5         WAITING  0.0       0.000     0:0.015   false     true
17   arthas-NettyHttpTelnetBootstr system         5         RUNNABLE 0.0       0.000     0:0.015   false     true
18   arthas-NettyWebsocketTtyBoots system         5         RUNNABLE 0.0       0.000     0:0.015   false     true
19   arthas-NettyWebsocketTtyBoots system         5         RUNNABLE 0.0       0.000     0:0.015   false     true
20   arthas-shell-server           system         5         TIMED_WA 0.0       0.000     0:0.000   false     true
21   arthas-session-manager        system         5         TIMED_WA 0.0       0.000     0:0.000   false     true
22   arthas-UserStat               system         5         WAITING  0.0       0.000     0:0.000   false     true
24   arthas-NettyHttpTelnetBootstr system         5         RUNNABLE 0.0       0.000     0:0.109   false     true
25   arthas-command-execute        system         5         RUNNABLE 0.0       0.000     0:0.015   false     true
10   t1                            main           5         BLOCKED  0.0       0.000     0:0.000   false     false
11   t2                            main           5         BLOCKED  0.0       0.000     0:0.000   false     false
12   DestroyJavaVM                 main           5         RUNNABLE 0.0       0.000     0:0.156   false     false

2, perform “thread -b” command , Find out which threads are currently blocking other threads , That is, the culprit of deadlock

[[email protected]]$ thread -b

"t1" Id=10 BLOCKED on [email protected] owned by "t2" Id=11
    at test.Deadlock.lambda$main$0(Deadlock.java:24)
    -  blocked on [email protected]
    -  locked [email protected] <---- but blocks 1 other threads!
    at test.Deadlock$$Lambda$1/250421012.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:748)

notes : The above command directly outputs Thread causing deadlock ID, And specific code locations , And the total number of threads blocked by the current thread :“<—- but blocks 1 other threads!“.

3, Other thread commands :

thread –all,  Show all threads ;
thread id,  Show the run stack of the specified thread ;
thread –state: View threads in the specified state , Such as :thread –state BLOCKED;
thread -n 3: Show the current busiest front N Thread and print stack ;
原网站

版权声明
本文为[qq_ thirty-seven million two hundred and seventy-nine thousand ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230623345111.html