JAVA CPU100% positioning and thread deadlock

Foreword

CPU100% and thread deadlock is caused by the system is running slow, one of the reasons of suspended animation. Under here to explain how to position if this occurs.

CPU100% Positioning

CPU100% Environmental Simulation

First, we give the following code to simulate the CPU100%

public static void main(String[] args) {
    cpuTest();
}

private static void cpuTest() {
    new Thread(() -> {
        while (true) {
            new Object();
        }
    }, "CPU-100").start();
    while (true) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                try {
                    new Object();
                    long random = new Random().nextInt(200);
                    Thread.sleep(random);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
复制代码

We want to simulate the scene is a cycle of death has been busy thread hidden among the many threads.

Program logic: infinite loop continues execution thread starts a named CPU-100, additional main thread start 10 threads 200 ms intervals.

Positioning thread

Implementation of the top command, find the total usage java process CPU 105.6%, for the process id 22024

Perform top -H -p 22024 view the situation this process CPU usage of all threads and so on. Which thread id 22041 CPU utilization is 99.9%.

Execution jstack 22024> stack.log, the thread stack dump information java process to stack.log

View stack.log, you can view the current thread information, including the name of the thread, the thread ID, process status. Nid that is where we find the front of the thread id, but it in hexadecimal display. 22041 turn is 0x5619 hex

We search 0x5619 which can target specific threads, and then bit into specific program code

Thread deadlock positioning

jstack can view the status of the thread, so it can be positioned deadlock.

Environmental Simulation Deadlock

First, we give the following code configured deadlock, the deadlock thread named deadLock-1, deadLock-2

public static void main(String[] args) {
    lockTest();
}

private static void lockTest() {
    Object o1 = new Object();
    Object o2 = new Object();
    new Thread(() -> {
        synchronized (o1) {
            try {
                Thread.sleep(500);
            } catch (Exception e) {
                e.printStackTrace();
            }
            synchronized (o2) {
                System.out.println("deadLock-1");
            }
        }
    },"deadLock-1").start();
    new Thread(() -> {
        synchronized (o2) {
            try {
                Thread.sleep(500);
            } catch (Exception e) {
                e.printStackTrace();
            }
            synchronized (o1) {
                System.out.println("deadLock-2");
            }
        }
    },"deadLock-2").start();
}
复制代码

Positioning thread

jcmd find the process id 41579

jstack 41579, found deadLock-1, deadLock-2 thread is blocked, the bottom will give information directly deadlock

Reproduced in: https: //juejin.im/post/5cfde465f265da1b897ac430

Guess you like

Origin blog.csdn.net/weixin_33843947/article/details/91470039