A programmer’s job is to spend most of his time writing code, because he spends a lot of time debugging code. You’re either debugging code or you’re about to debug code. 🙂


Today we are going to talk about some tips for debugging code and some quick ways to locate problems when using the DEBUgger provided by the IDE.


See here friends, don’t immediately think I clickbait, read on, if you have some more useful techniques, welcome to leave a comment.


Let’s get down to business.



1 Multithreading Debugging


If you have developed multithreaded applications, you will know that sometimes, in order to observe the different states of variables between multiple threads, and lock acquisition, you will think of adding a breakpoint debug to the code.


When a breakpoint stops in the IDE, you can switch to another thread and run other code without affecting each other. Of course, there is a switch, which is turned on by default in Eclipse,


However, IntelliJ IDEA is disabled by default. That is, if you add breakpoints to your IDEA code, even if one thread breaks, the other thread has already executed. Debugging of multithreaded applications can also begin after setting thread suspend to Eclipse mode. A simple setup can change suspend to Thread directly at the breakpoint



Once a thread is down, it can continue running in another thread by switching in the thread window.

IntelliJ IDEA switch here



We see that the main thread and the pool-1-thread-1 thread are both in the RUNNING state. Switching to either thread can continue.


At this point, you can write a multithreaded application that simultaneously stores content in non-thread-safe containers such as ArryList, and then observe vividly why they are thread-unsafe and what goes wrong.


PS: By the way, this is why creating a thread with a meaningful name is recommended, at least a recognizable name, otherwise there is no way to tell which thread is your own, and it is difficult to switch.



2 Backward Execution


When we say backward execution, sometimes when we debug code, each method is in a single step, you will see a change in the value of a variable, one method does not follow, the value changes, and you have to start again.


Depending on the function performed by retreating, you can retreat, just like the function of playing chess. Of course, I came up with the name for the backward treat, which is called Drop Frame in the IDE.




With this feature, after dropping the current Frame, the changed variable value will not be restored. For example, if you pass in a List to the currently called method and add content to the List within the method, the List will not return to its previous state when dropped to the place where the method was called.


But when the method is called again, you can observe when the List has changed, at least without having to run the program again. You can even step back at a word.


3 Conditional breakpoint


To debug the code, you need to add breakpoints where you want to observe changes, and then execute them carefully. But if it is in a loop, or the method will be called by multiple threads at the same time, you carefully to the single step debugging, found that there is no content you care about, and ran down from the beginning of the loop, or no your content, people began to irritable up.


When you add a breakpoint, you can add conditions to the breakpoint so that the breakpoint takes effect only when the conditions specified are met.


In IntelliJ IDEA, right click on the breakpoint, the following image will pop up the condition box, enter the specified condition.


With conditional breakpoints, irrelevant and uninteresting code can be skipped.



4 Snippet Code


I don’t have a name for it. Sometimes during debugging, you want to invite a piece of code related to this, but not in the source file, to look at it for problem analysis. Do you stop the program, add code, and run again?


IDEA has the ability to execute snippets of code that you wrote temporarily in the context of the current code.


For example, if the current method passes in a List, but one element is missing from the method that you need for a later condition, you can temporarily add one to the List using the function performed by the snippet code.


Note that in IntelliJ IDEA, the button with the red box above like a small calculator is the function to be temporarily implemented. After clicking, the Evaluate Expression box below the button will pop up. Enter the code and click Evalute in the lower right corner. The return value is displayed at Result.



This is equivalent to temporarily changing the variable content.


You can then call the object’s methods, perform functions, get property values, and so on.



5 View the variable value


Every IDE provides a window to view the value of the current context variable when debugging code. In addition to viewing basic types that are not final, you can change their values directly here. In this way, if the loop is executed multiple times, the desired value can be restored each time without restarting the program.




If you find this article’s title confusing, please share the tips you think are good during debugging. 🙂


For the principles of Java debugging, see the previous article: Debug Implementation Principles


Find this article helpful? Please share it with more people

Follow “Tomcat stuff” for more great articles! Understand the principles and answers behind common questions. In-depth source code, analysis details, original content, welcome to pay attention to.