I heard that wechat search “Java fish” will change strong oh!

This article is in Java Server, which contains my complete series of Java articles, can be read for study or interview

(1) Introduction

There are two best ways to solve code problems that you can’t see at first glance. The first is logging, and the second is dubugs. However, I have found that many programmers can only type a simple ordinary breakpoint, which completely fails to play the power of IDEA. In this installment, I bring the debug tutorial that I think is the most useful in IDEA.

(2) Introduction to debugging buttons

When you open Debug mode, you will see the following buttons, which I will introduce one by one:

Show Execution Point (Alt + F10) : Jump back to the current line of Execution

2, Step Over (F8) : line by line, do not enter the inner method.

Step Into (F7) : If the current line has a method, enter the method inside. Note: Generally used to enter custom methods, not into the official class library methods.

4, Force Step Into (Alt+Shift+F7) : The difference with Step Into is that you can enter any method.

5, Step Out (Shift+F8) : Exit from Step Into method to call the method, the method is finished.

Drop Frame: falls back to the last method call

7, Run to Cursor (Alt+F9) : Run to Cursor (Alt+F9

Evaluate Expression (Alt+F8) : Evaluate Expression, Evaluate the value of the current variable or Expression, convenient debugging.

1.Rerun: Rerun the program.

Resume Program (F9) : Run to the next breakpoint or stop (if the next breakpoint is not available).

E.g. < 1 > Pause Program:

4. Stop (Ctrl+F2) : Close the program

5. View Breakpoints (Ctrl + Shift + F8

6, Mute BreakPoints: after selected, you can disable BreakPoints

(2) Introduction of breakpoint types

2.1 Common Breakpoints

The most commonly used breakpoint type can be opened by left-clicking on the right of the serial number bar. After debug mode is enabled, the breakpoint is first encountered.

2.2 Detailed breakpoints

The detailed breakpoint can be displayed with Shift+ left key. The detailed breakpoint is some configurations of the breakpoint, such as whether to enable, Suspend, Condition, and Log output. This is an upgraded use of a regular breakpoint, where you can also right-click to expand the page.

2.3 Method breakpoints

A method breakpoint is a breakpoint on a method. It is shaped like a diamond. Method breakpoints can be used to test the data of a method by stopping after running into the method and before leaving the method. In addition, if a method breakpoint is hit on an interface method, it is automatically entered into the implementation class of the interface method.

2.4 Abnormal Breakpoint

Breakpoints (Ctrl + Shift + F8) : Open View Breakpoints (Ctrl + Shift + F8). Click the plus sign to select Java Exception BreakPoints.

Entering the type of exception is automatically added to the exception breakpoint list

When you look at the results, you end up with the first exception you encounter. To prevent null pointer exceptions, change the code to “aaa”.equals(name).

2.5 Monitoring Breakpoints

We can monitor changes in the value of a variable. This breakpoint is called a monitor breakpoint. The monitor breakpoint is hit on a variable and is shaped like an eye.

Run the code, Resume Program (F9), and it will stop every time the name value changes.

(3) multithreading breakpoint

Breakpoints are usually used to block the entire program, which is inconvenient when debugging multithreading. Idea also provides breakpoints that block only the current thread. First write a simple multi-threaded code, code implementation of the content is: two threads every second output a thread name and several seconds of execution

public class MultiThreadBreakPoint {
    public static void main(String[] args) {
        // The first thread runs
        new Thread(()->{
            int i=0;
            while (true){
                System.out.println(Thread.currentThread().getName()+":"+i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                i++;
            }
        }).start();
        // The second thread runs
        new Thread(()->{
            int i=0;
            while (true){
                System.out.println(Thread.currentThread().getName()+":"+i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                i++;
            }
        }).start();
        System.out.println("I'm the main thread"); }}Copy the code

Debug blocks the entire program whenever normal breakpoints are made anywhere in the code (main thread, thread 1, and thread 2).

Now for multithreaded breakpoints, this breakpoint blocks only the current Thread as long as Suspend is set to Thread in the verbose breakpoint (or right click on the normal breakpoint).

Now I have a thread breakpoint on the main thread. The main thread is blocked, but the other two threads are still running:

Therefore, we can make a thread breakpoint in each child thread, and then select which thread to debug in the Frame, you can make your own decision on the execution order of different threads, which is very convenient to reproduce the multithreading problem.

(4) Summary

Breakpoint debugging is a programmer must master the skill, learning cost is low, 20 minutes basic learn, but the effect is great. I am fish boy, next time to talk about another tool for solving code problems —- log!