1

This post Communicating between threads section says that

The issue is particularly acute if the task on the new thread wishes to modify views associated with main UI thread, because it is strictly forbidden to do so directly.

Why is it prohibited/forbidden.

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
Ashwin
  • 12,691
  • 31
  • 118
  • 190
  • Composing the UI would be a pain in the ass if they also had to make it thread-safe.. measuring & layout - followed by draws - would have to be protected from updates. It makes sense from a design point-of-view to limit updates to occur between draws. – Jens May 14 '12 at 10:12
  • @Jens : So how does handler ensure thread safety? – Ashwin May 14 '12 at 10:13
  • 1
    The `Handler`, if created on the UI-thread will use the `Looper` associated with the UI-thread. It's however pretty easy to create a `Handler` using an `android.os.HandlerThread` that is *unable* to update the UI. – Jens May 14 '12 at 10:17

3 Answers3

1

Please read this,

Its always a good practice to keep the UI work on UI Thread, and Non-UI work on Non-UI Thread, and since HoneyComb its a law. We always start with the Dedicated UI Thread, but as soon as we create another thread to do some Non-UI task, we get dropped of the Dedicated UI Thread, and lands on a Non-UI thread, once when we finish our work and want to put the processed Output on the UI thread, its not possible directly, because we have lost the reference to the UI thread. Now Handler helps in getting the reference to the thread in which it was created. So its a good practice to initialize the Handler in onCreate(), and then later call this reference from within the non-ui thread.

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
1

The Handler helps queue requests from worker threads in a MessageQueue so they are executed sequentially on the main thread.

The following blog post is quite helpful to learn more about how Handlers work:

Android – Multithreading in a UI environment

Gunnar Karlsson
  • 28,350
  • 10
  • 68
  • 71
  • that was a very good link. It says that messages are put in the queue. Ans then they are looped over by the loper and executed one by one. How can messages be executed. Did they mean that the updateui(message) is placed in the queue to be lopped over by the looper and executed? – Ashwin May 14 '12 at 11:13
  • Yes, that's how I understand it. Here's another quite helpful link on the subject: http://techtej.blogspot.com/2011/02/android-passing-data-between-main.html – Gunnar Karlsson May 14 '12 at 11:19
  • that link was even better! I noticed in your profile that you are an android developer. can you help me with this question http://stackoverflow.com/questions/10548530/validating-and-reading-a-word-file-in-android ? – Ashwin May 14 '12 at 11:33
  • I've posted an answer. Hope it helps. – Gunnar Karlsson May 14 '12 at 13:56
0

Use AsyncTask<> provided by android, it synchronizes the UI and Non-UI threads

Methods in AsyncTask<>

doInBackground(String...) // Work on the Non-UI thread

postExecute(String result) // Getting the Output from the Non-Ui thread and

Putting the Output back on the UI Thread

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75