zoukankan      html  css  js  c++  java
  • 线程运行诊断

    线程运行诊断**

    定位
    用top定位哪个进程对cpu的占用过高

    ps H -eo pid,tid,%cpu | grep 进程id (用ps命令进一步定位是哪个线程引起的cpu占用过高)

    jstack 进程id 可以根据线程id 找到有问题的线程,进一步定位到问题代码的源码行号

    要先将32665转为16进制,即0x7f99,定位到了具体的代码

    /**
     * 演示 cpu 占用过高
     */
    public class Demo1_16 {
    
        public static void main(String[] args) {
            new Thread(null, () -> {
                System.out.println("1...");
                while(true) {
    
                }
            }, "thread1").start();
    
    
            new Thread(null, () -> {
                System.out.println("2...");
                try {
                    Thread.sleep(1000000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "thread2").start();
    
            new Thread(null, () -> {
                System.out.println("3...");
                try {
                    Thread.sleep(1000000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "thread3").start();
        }
    }
    
    

    案例2:程序运行很长时间没有结果

    运行java程序,很长时间没有结果,使用

    jstack 32752

    在最后可以看到出现了死锁

    /**
     * 演示线程死锁
     */
    class A{};
    class B{};
    public class Demo1_3 {
        static A a = new A();
        static B b = new B();
    
    
        public static void main(String[] args) throws InterruptedException {
            new Thread(()->{
                synchronized (a) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (b) {
                        System.out.println("我获得了 a 和 b");
                    }
                }
            }).start();
            Thread.sleep(1000);
            new Thread(()->{
                synchronized (b) {
                    synchronized (a) {
                        System.out.println("我获得了 a 和 b");
                    }
                }
            }).start();
        }
    
    }
    
    
  • 相关阅读:
    1055. [HAOI2008]玩具取名【区间DP】
    BZOJ2435:[NOI2011]道路修建 (差分)
    1084. [SCOI2005]最大子矩阵【网格DP】
    1060. [ZJOI2007]时态同步【树形DP】
    1050. [HAOI2006]旅行【并查集+枚举】
    2463. [中山市选2009]谁能赢呢?【博弈论】
    luogu P1195 口袋的天空
    luogu P1162 填涂颜色
    luogu P1223 排队接水
    luogu P1331 海战
  • 原文地址:https://www.cnblogs.com/heliusKing/p/11999927.html
Copyright © 2011-2022 走看看