Steps to troubleshoot Java deadlock and Java CPU 100%

Source: Internet
Author: User
Tags jconsole

工欲善其事, its prerequisite

Brief introduction

This article two troubleshooting simple tips, one is the Java deadlock troubleshooting, this general in the interview will ask, if there is no multi-threaded, the actual opportunities encountered, the second is the Java CPU 100% troubleshooting, this actual development, the application of the line of the problem is more likely to occur, So here is a brief summary of the study of their own knowledge of a collation, improve their ability to solve problems.

One, Java deadlock troubleshooting

We're going to think about three questions through the headline:

    1. What is a deadlock?
    2. Why is there a deadlock?
    3. How do I troubleshoot a deadlock in my code?

As a technician (engineer), in the face of problems, the ability that may be needed is how to solve the problem. But when learning technical knowledge, it is necessary to ask why, we must exercise their ability in this area, so as to better grasp the knowledge.

Answer :

    1. What is a deadlock?

      Deadlock refers to two or more processes in the course of execution, because of competing resources or due to the communication between each other caused by a blocking phenomenon, if there is no external force, they will not be able to proceed. At this point the system is in a deadlock state or the system generates a deadlock, and these processes, which are always waiting on each other, are called deadlock processes. Baidu Encyclopedia: Deadlock

Note: Deadlocks can occur for both processes and threads, as long as the deadlock condition is met!

    1. Why is there a deadlock?

      From the concept above, we know
      (1) must be two or more than two processes (threads)
      (2) must have competitive resources

    2. How do I troubleshoot a deadlock in my code? "The point is coming. "
      First write a deadlock code, see example:
      ```
      /**
  • Use Jstack to troubleshoot deadlocks
  • @author Dufyun
  • */
    public class Jstackdemo {
    public static void Main (string[] args) {
    thread T1 = new Thread (new Deadlocktest (true));//Create a thread
    Thread t2 = new Thread (new Deadlocktest (false));//Build another thread
    T1.setname ("thread-dufy-1");
    T2.setname ("Thread-dufy-2");
    T1.start ();//Start a thread
    T2.start ();//Start another thread
    }
    }
    Class Deadlocktest implements Runnable {
    Public boolean falg;//Control thread
    Deadlocktest (Boolean falg) {
    This.falg = Falg;
    }
    public void Run () {
    /**
    * Call the T1 thread if the value of Falg is True
    */
    if (FALG) {
    while (true) {
    Synchronized (DEMO.O1) {
    System.out.println ("O1" + thread.currentthread (). GetName ());
    Synchronized (DEMO.O2) {
    System.out.println ("O2" + Thread.CurrentThread (). GetName ());
    }
    }
    }
    }
    /**
    * Call the t2 thread if the value of Falg is False
    */
    else {
    while (true) {
    Synchronized (DEMO.O2) {
    System.out.println ("O2" + Thread.CurrentThread (). GetName ());
    Synchronized (DEMO.O1) {
    System.out.println ("O1" + thread.currentthread (). GetName ());
    }
    }
    }
    }
    }
    }

Class Demo {
Static Object O1 = new Object ();
Static object O2 = new Object ();
}

```
When the above code executes, there will be a deadlock, then the following methods are used to troubleshoot:

Using JPS + Jstack

First: In the windons command window, use jps-l "Do not use JPS Please check the information yourself"

Second: Use jstack-l 12316 "Do not use jstack Please check the information yourself"

Using Jconsole

Opening jconsole,jconsole in window is a graphical monitoring tool!

In the windons command window, output JConsole

Using the Java Visual VM

Opening JVISUALVM,JVISUALVM in window is a graphical monitoring tool!

In the windons command window, output JVISUALVM

Second, Java CPU 100% Troubleshooting

This if in the actual application development encountered, how to troubleshoot?

There's no step-by-step diagram of the process, only a simple operation! Be free to write a detailed example.

1. Use the top command to view the higher CPU resource-intensive PID

2, through JPS find the current user under the Java program PID

Execution Jps-l can print out all of the application's PID, find a PID and use the same CPU as the 100% id!! I know which service it is.

3. Using Pidstat-p

4. Find a thread with high CPU consumption tid

By finding that the 3467 Tid occupies a larger cup

5. Convert Tid to 16 binary representation

Convert 3467 to hex d8d, note that it is lowercase!


6, through the Jstack-l

Use Jstack to output thread DUNP information for current PID

7. Find the corresponding thread of the TID (the thread ID of the output is 16 binary), find the corresponding code

Iii. Stress Testing Use Jstack to find code performance issues for your system

1, when the pressure test, using JPS to find the application of PID
2. Then use Jstack to output the dump information applied during the stress test.
3, analysis output log file that method block thread occupies the most, here may be a performance problem, find the corresponding code analysis

Reference

1, Java application CPU Usage 100% reason analysis
2, [Java] CPU 100% Cause lookup resolution
3, on-line application troubleshooting series
4. Analyzing the problem of high CPU consumption in Java application

If you think this blog is helpful to you, please praise or like, let more people see, thank you!

If handsome (beautiful), Wise (Smart), and I as simple and good you see this blog post there are problems, please point out, I humbly accept you let me grow criticism, thank you for reading!
I wish you a happy day!

Welcome to visit my csdn blog, we grow together!

No matter what you do, just stick to it and see it differently! On the way, decently!

Blog home: http://blog.csdn.net/u010648555

Steps to troubleshoot Java deadlock and Java CPU 100%

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.