About Android UI is not thread safe

Source: Internet
Author: User

Today occasionally on the Internet to see this article http://hold-on.iteye.com/blog/991403

1, as we all know, in the development of Android, non-UI threads cannot manipulate controls in the UI thread, that is, the UI is not thread safe;

2, but when the setprogress (int count) method of the ProgressBar control is invoked in a worker thread (not a UI thread), the program runs and

There is no exception,

3, but when the call to SetBackground (int color) or setvisibility (int v), the program is running after the exception

4, the first and 3rd said, but the 2nd can not understand

Question: Who can define "manipulating the main thread UI in a worker thread", such as Setprogress on the operating UI? What is the principle of general determination.

In fact, this is a typical multi-threaded security problem, of course, in Android can not be listed outside. In most applications, we typically have a UI thread, the Android UI Looper thread, and a communication thread that is responsible for interacting with the server side.

So why is it possible to invoke ProgressBar in a non-UI thread? Because the ProgressBar control is a variable of a non-UI thread, it is all of a non-UI thread, there is no problem with multi-threaded shared data, and methods such as setbackground (int color) are invoked in a non-UI thread. It involves the problem of invoking variables such as the context of the UI thread, which can have thread-safety implications.

In fact, many of the basic principles of computer science are interlinked, nothing more than to transform an implementation of the point of view, the problem needs to think more.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.