开发者

Manipulating the UI from a worker thread

The Andoid UI toolkit is not thread-safe, when you try to modify the UI from a worker thread you get 开发者_开发知识库the CalledFrowWrongThreadException (or something like that).

Moreover, the dev guide say:

[Manipulating the UI from a worker thread] can result in undefined and unexpected behavior, which can be difficult and time-consuming to track down.

But it does not seem to me very difficult to understand what a CalledFrowWrongThreadException mean.

Does the documentation was written before the introduction of CalledFrowWrongThreadException or are there still cases where the exception is not thrown? (or where the error is indeed difficult and time-consuming to track down)


The main issue here is that checking what thread is calling a given function incurs a processing overhead; there are almost certainly calls that don't check for precisely this reason - it would slow down the UI rendering.

By extension, the reason UI functions aren't thread-safe is that if you through mutex code into the equation, the performance hit becomes even greater.


Just like Swing in Java, and WinForms in .NET, there may be cases where the method in question does not check to determine that you are calling from the proper thread, and therefore does not necessarily throw the expected exception.

Providing the above documentation is a way to cover themselves and explain a simple idea that actually requires a lot of effort to work around if you do not know it, or understand it, before the wrong thread exception is ever thrown by offending code, which may be much later down the road.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜