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.
At the same time, I also organized a lot of Java materials here. Content includes: Spring, Dubbo, MyBatis, RPC, source code analysis, high concurrency, high performance, distributed, performance optimization, micro services advanced architecture development and so on.
Friends in need can click:This! Point the, code word: J j.
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. Perform the rollback
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 of the retreat host, which is called in the IDEDrop Frame.
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 breakpoints
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.
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 changed value of variables
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.Share what you think is a good technique in the debugging process, welcome to share in the comments.
The last
There are Java core knowledge points + a full set of architect learning materials and video + first-line factory interview gem + resume template can be received + Ali Meituannetease Tencent Xiaomi IQiyi Quick hand bilibilibilii interview questions +Spring source code collection +Java architecture practice ebook.
Friends in need can click:This! Point the, code word: J j.