Source: Tomcat (wechat id: Tomcat0000)

Author: Hou Shucheng

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, the name for this backward implementation is my own and is called Drop Frame in the IDE.

With this function, after dropping the current Frame, the execution of the program will fall back to the previous function.

Note, however, that changed variable values are not restored. For example, if you pass a List to the current calling method and add new data to the List within the method, even if you Drop Frame back to the previous function, the List still contains the added data and does not revert to the previous state.

But when the method is called again, you can watch when the List has changed, at least without having to run through the program again, or even back off 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. 🙂



What you have seen is just the tip of the iceberg. For more exciting articles, see “Code farmers turn over 2016 Article highlights”.

Any tips you’d like to share? Welcome to contribute! My contact information: Wechat: Liuxinlehan QQ: 3340792577

Public number: code farmer turn over

The account was created by a former IBM architect who worked for 15 years to share lessons learned in programming and the workplace.



The editors recommend

Digg is a high quality technology community, from mobile development to architecture, programming languages to open source libraries, so that you don’t miss every technical dry matter of Internet development. Long press the picture qr code to identify or search for “gold digging” in the major application market, the technology is in your grasp.