Non-thread security of Android UI

Source: Internet
Author: User

I occasionally see this on the internet todayArticleHttp://hold-on.iteye.com/blog/991403

1. We all know that in Android development, non-UI threads cannot operate controls in the UI thread, that is, the UI is non-thread-safe;

2. When the setprogress (INT count) method of the progressbar control is called in the working thread (non-UI thread,ProgramRun and

No exception,

3. If setbackground (INT color) or setvisibility (int v) is called, an exception occurs after the program runs.

4. Let's talk about the first and third points, but the second point cannot be understood.

Problem:Who can give a definition under "operate the main thread UI in the working thread", for example, is the preceding setprogress used to operate the UI? What is the general determination principle?

In fact, this is a typical multi-threaded security issue, of course, it cannot be excluded from Android. In most applications, we usually have a UI thread, that is, the android UI logoff thread, and a communication thread, responsible for interacting with the server.

So why can I call progressbar in a non-UI thread? The progressbar control is a variable of a non-UI thread. It is not owned by a non-UI thread and does not involve multi-thread data sharing. Instead, it calls methods such as setbackground (INT color) in a non-UI thread, it involves calling the context and other variables of the UI thread, so there is a thread security risk.

In fact, many basic principles in computer science are the same. They are nothing more than changing the implementation perspective. You need to think more about the problems you encounter!

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.