Android UI Thread Safety

Source: Internet
Author: User

Reprint http://blog.csdn.net/dxpqxb/article/details/7987764

We often see a word: The main thread of Android is threads that are unsafe. This means: The update UI can only be the main thread of the work, the child thread update UI is thread insecure, so Android non-main thread operation of the main UI will be error. Why is it? Because there may be multiple child threads, multiple threads manipulating one control at a time may conflict, so Android restricts only the main thread to manipulate the UI. The child threads want to manipulate the UI, and you can tell me (the main thread) that I have to update.

When an Android program starts running, a process is started separately. By default, all activity or service (service and activity) in this program is only two of the components that Android provides, in addition to content provider and broadcast Receiver) will run in this process space.

An Android program has only one process by default, but it can have many threads thread.

Among so many thread, there is a thread, which we call the UI Thread. The UI thread is created when the Android program is running, and is the main thread of a process that is responsible for controlling the display, update, and control interaction of the UI interface. At the beginning of the Android program, a process presented a single-threaded model, with all tasks running in one thread. Therefore, we think that every function that the UI thread executes should take as short a time as possible. Other more time-consuming work (access to the network, download data, query database, etc.), should be left to the sub-thread to do so as not to block the main thread. (If the main thread is blocked for 5 seconds, it pops up a warning that the application is not responding and is waiting or shutting down.) )


So how does the UI thread work with the other thread? Common methods are:

Create a new handler instance of the main thread as listener to allow the child thread to push the data to the main thread's message quene to trigger the Handlermessage () function of the main thread, let the main thread know the state of the child threads, and update the UI on the main thread.


For example, when the state of a child thread changes, we need to update the UI. If the UI is updated directly in a child thread, the following exception is usually thrown:

Error/javabinder (1029): Android.view.viewroot$calledfromwrongthreadexception:only The original thread that created a View hierarchy can touch its views.

This means that the UI cannot be updated in the child thread. To do this, we need to update the interface with the handler instance, notifying the main thread UI thread.

As below, first create a handler on the main thread to listen for message events:

     Private final int update_ui = 1;           Private Handler Mhandler = new MainHandler ();             Private class MainHandler extends Handler {@Override public void Handlemessage (Message msg) {                Switch (msg.what) {case UPDATE_UI: {log.i ("Ttsdeamon", "update_ui");                Showtextview.settext (Edittext.gettext (). toString ());                     ShowAnimation ();                 Break             } Default:break; }}} private Handler Mhandler = new Handler () {@Override public void Handlemessage (Message msg)                {switch (msg.what) {case UPDATE_UI: {log.i ("Ttsdeamon", "update_ui");                Showtextview.settext (Edittext.gettext (). toString ());                     ShowAnimation ();                 Break             } Default:break; }          }}//When the state of the child thread changes, a message is emitted in the child thread informing the main thread to update the UI. Mhandler.sendemptymessagedelayed (update_ui, 0);

  

Android UI Thread Safety

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.