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

99封情书 提交于 2021-02-08 23:44:17

问题


I'm writing an Android app in Eclipse, and displaying some text on screen using a TextView. A different thread sometimes changes the text of this view, with the following code:

runOnUiThread(new Runnable() {

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

I wanted to debug the setText line to check that the correct String was being passed in (as for some text, it was not being word-wrapped to fit the screen, and for other text it was, but that's a different issue). When I put a breakpoint on that line, the text is always word-wrapped. And when I disable/remove the breakpoint, the text that usually doesn't get word-wrapped (it's deterministic) is not word-wrapped.

This doesn't make much sense to me. I've seen this question, and I understand the reasoning behind that, but I can't see why that would be the issue here. Can anyone shed some light on this? I'm testing on a device, if that makes any difference.


Edit: It turns out it was a race condition, now I just have to work out how to fix it... Obviously I could only pick one 'correct' answer, but thank you to all of you for the helpful comments :)


回答1:


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

Putting a breakpoint eliminates the race (so a "rogue" thread that was overriding the value by running later is now finished by the time your breakpoint is caught, as an example).

Or it's a clear case of Heisenbug :)




回答2:


Try setting setting the property Halt VM for your breakpoint. The default behaviour is to just halt the running thread. If you are getting "contamination" from other threads this should stop that from happening.




回答3:


It is possible, since your code run in its own thread, that the code that sets up the screen (and hence, decides or not to wordwrap) would run before, or after your call, since it runs in another thread.

For example, in TextViews, events will fire off and be caught by OnEditorActionListener.



来源:https://stackoverflow.com/questions/7178753/in-java-eclipse-why-does-setting-a-breakpoint-in-my-code-alter-the-effect-of-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!