During the project development, I found that many friends could use IDEA to write codes very skillfully, but they did not have a good command of IDEA debugging skills, only knowing basic debugging functions such as F7, F8 and F9. For complex debugging scenarios, such as debugging a particular value in a for loop; Multithreading, Reactor debugging; Modify the running value of a variable, etc. Here we will introduce the debugging skills of IDEA in different scenarios. The efficiency of debugging bugs must be greatly improved after watching.

1. Conditional breakpoints

This technique is often used in loops, such as when you want to stop a breakpoint at a particular value while iterating through a large List.

Conditional breakpoints

If you right-click on the red dot next to the breakpoint, it will display a screen. If you place a breakpoint Condition in Condition, it will automatically stop at I =10.

Conditional breakpoint debugging

2. Go back to the “previous Step”

This technique is most suitable for a particularly complex method set method scenario, it is not easy to run, accidentally shake your hand, the breakpoint has passed, want to go back to look at the variable value, if you do not know the technique, can only run again.

Method1 calls method2 with the current breakpoint j=100. When you click on the Drop Frame icon at the red arrow in the above image, time passes

Step back

Back to method1, when I first called it, the variable I changed to 99. Curiosity is the ladder of human progress, and if you want To know why it’s called Drop Frame, as opposed To something like Back To Previous, you can read the JVM, which stores the thread’s running status in stacks, and Drop Frame means To discard the stack Frame that’s currently running. The position of the current “pointer” will automatically be the position of the previous frame.

3. Multithreading debugging

When multithreading is running at the same time, who executes first, who executes after, completely depends on the CPU mood, unable to control the sequence, there may be no problem when running, but debugging is more trouble, the most obvious is the breakpoint jumping, while stopping this thread, while stopping in another thread, such as the following figure:

If you were expecting the next break point to be verse 2, you might be disappointed:

If you want a thread to stop on any thread it wants during debugging, you can right click on the red dot at the three breakpoints in the figure.

Suspend suspends on a per-thread basis, not All. After setting all three breakpoints like this, try again

Note the position of the red box in the image above. When the breakpoint is stopped, the drop down box shows the individual threads (note: it’s a good habit to give threads easily recognizable names!). , we can select thread “bird in the sky”

Breakpoint stops at the second line of the poem.

4. Remote debugging

This is also a powerful tool to install B, the local machine does not need to start the project, as long as there is a source code, you can debug the code on the server directly in the local machine, open the pose as follows:

4.1 When the project is started, remote debugging is allowed first

java -server -Xms512m -Xmx512m -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=9081 -Djava.ext.dirs=. ${main_class}
Copy the code

\

What works is that

\

-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=9081
Copy the code

\

${main_class} = ${main_class} = ${main_class} = ${main_class} = ${main_class} = ${main_class

4.2 Idea Setting Remote debugging

Then you can debug

If you have the source code for your project, you can make a breakpoint where you need it, and then try accessing a remote URL, and the breakpoint will stop.

5. Temporarily execute the expression/change the running value of a variable

When debugging, you can temporarily execute some expressions, as shown below: click on either icon

After clicking the + sign, you can enter an expression, such as I +5, into a new input field

And then enter, and immediately you see the result

Of course, if you want to change the Value of a variable dynamically while debugging, you can easily right-click on the variable, select Set Value, and the rest is history.

Well, that’s all for today’s article. Keep these 5 debugging tips in mind to help you quickly locate problems and solve bugs. Finally, I wish you all a happy debug!