0

Today I managed to speed-up my Android application a lot by using a Runnable handling a piece of code inserting/updating and deleting some database stuff. However I also used RunOnUiThread() and a normal Thread() but I don't have any idea what the differences are between all three. I do know the ASyncTask, but how do you choose what to work with and what are the main differences?

An explanation / link to another site would be very nice.

Kind Regard.

  • 1
    `Thread` runs **off** the `MainThread i.e UI` and `runOnUIThread()` runs **on** the main thread! – Muhammad Babar Oct 06 '15 at 08:11
  • 1
    Did you read http://stackoverflow.com/questions/26938265/difference-between-thread-runnable-handler-runonuithread-asynctask? – Pankaj Kumar Oct 06 '15 at 08:12
  • 1
    @Jordy, [here](http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread) you can check the difference in using Runnable and Thread. As for RunOnUIThread - to apply changes in your View it should be invalidated and re-layout. And these actions can be performed only in the UI thread, so RunOnUIThread is a way to do it. – DmitryArc Oct 06 '15 at 08:32
  • You guys already made some things clear for me. Thanks you for this. Just a small question, I can't figure out what actions will take a lot of recourses. For example, I do know making actions on my database will need more recourses but that's it. What else is "heavy"? –  Oct 06 '15 at 09:14

2 Answers2

0

When you are modifying some value on UI like (textBox.text) from normal thread it raises an exception. So you have to use RunonUiThread() there to share values with UI and run asynchronously at the same time.

normalThreadMethod(){
textBox.text = "Test";  //Exception   
}

RunOnUIThread(){
textbox.text = "Test"; //no error
}
pavinan
  • 1,275
  • 1
  • 12
  • 23
  • Thank you for your explanation, I do understand the purpose of RunOnUiThread. Didnt state this in my question. –  Oct 06 '15 at 09:15
0

Most of the code you write runs on the UI thread, ie main thread, by default. Some operations about Views must be executed on the UI thread. And operations which consume lots of resources should be executed off the UI thread.

You can start a new Thread by calling new Thread(Runnable).start(), then the task will be executed on a non-UI thread. But it's recommended to use a thread pool like ExecutorService to do this, because it reuses the threads.

For an AsyncTask, the code in doInBackground() runs on a non-UI thread from a static thread pool of AsycTask, while onPostExecuted() are executed on the UI thread. So you should do UI operations in onPostExecuted().

When using Handler, where the handleMessage() code runs is based on the Looper you pass to the constructor of Handler. By default, it's Looper.getMainLooper(), so it runs on the UI thread.

ProtossShuttle
  • 1,623
  • 20
  • 40
  • Great answer, will look into the ExecutorService. For now your answer helped me the best. Thank you a lot. –  Oct 06 '15 at 09:17