Java – In Java/Eclipse, why does setting a breakpoint in my code change the effect of the statement?

In Java/Eclipse, why does setting a breakpoint in my code change the effect of the statement?… here is a solution to the problem.

In Java/Eclipse, why does setting a breakpoint in my code change the effect of the statement?

I’m writing an Android app in Eclipse and using TextView to display some text on the screen. Different threads sometimes change the text of this View using the following code:

runOnUiThread(new Runnable() {

    @Override
    public void run() {
        view.setText(newLyric);
    }
});

I want to debug setText the line to check if the string passed in is correct (as for some text, it is not wrapped to fit the screen, while for other text it is, but this is a different issue). When I put a breakpoint on that line, the text always wraps automatically. When I disable/remove a breakpoint, text that normally doesn’t wrap (deterministically) doesn’t wrap.

It doesn’t mean much to me. I’ve seen this question and I understand the reason behind this , but I don’t understand why this is the problem here. Can anyone shed light on this? I’m testing on the device, is there any difference.


EDIT: It turned out to be a race condition, now I just need to figure out how to fix it… Obviously I can only choose one “correct” answer, but thank you all for your useful comments 🙂

Solution

Things like this sometimes/often are a sign of a race condition.

Placing a breakpoint eliminates contention (for example, a “rogue” thread overrides the value by running later, now done when your breakpoint is caught).

Or obviously Heisenbug 🙂

Related Problems and Solutions