11

I am a beginner in android application development.I am working with threads in android.I have read about a runOnUiThread which run code on main UI(if i am not wrong?i guess.).

My question is what is the difference between normal code on main UI and code inside runOnIUThread.

Example:1

class A
{
getDataFromServer(foo);//Code on mainUI
}

Example:2

getActivity.runOnUiThread(new Runnable(){
@Override
public void run(){
getDataFromServer(foo);
}
});

What is difference in both example.Please help me.Your response will be a new learning for me.

Charuක
  • 12,953
  • 5
  • 50
  • 88
Mohit Gaur
  • 355
  • 1
  • 3
  • 22
  • 1
    If you want to update UI in non UI thread then `runOnUiTread()` is used. – D.J Jan 13 '17 at 10:40
  • if you dont want use runOnUiThread () then use AsyncTask , it have preExecute and postExecute to interact with UI. – Rv Lalwani Jan 13 '17 at 10:47
  • What if, i will update the UI form runOnUiThread can code inside thread goes to long process,will it block further execution(ANR) – Mohit Gaur Jan 13 '17 at 10:58

3 Answers3

14

Assuming that you meant simple code for UIThread code,

What is a thread ?

A thread defines a process running

First runOnUiThread ..

Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.

What is UIThread

  • Main thread of execution for your application
  • Most of your application code will run here onCreate, onPause, onDestroy, onClick, etc.

    So simply Anything that causes the UI to be updated or changed HAS to happen on the UI thread

When you explicitly spawn a new thread to do work in the background, this code is not run on the UIThread.Now what if you want to do something that changes the UI? Then you are welcome to runOnUiThread

You have to use runOnUiThread() when you want to update your UI from a Non-UI Thread. For eg- If you want to update your UI from a background Thread. You can also use Handler for the same thing.

Charuක
  • 12,953
  • 5
  • 50
  • 88
  • Can runOnUiThread blocks further exection, in other words can it create ANR if thread goes to long process? – Mohit Gaur Jan 13 '17 at 10:56
  • Well that depends. with your task..if there are any kind of a process you're supposed to use a Handler https://developer.android.com/guide/components/processes-and-threads.html#WorkerThreads t provides these background threads the ability to execute code that can modify the UI. They do this by putting the UI-modifying code in a Runnable object and passing it to the runOnUiThread method. – Charuක Jan 13 '17 at 11:04
  • Activity.runOnUiThread() is a special case of more generic Handlers. With Handler you can create your own event query within your own thread. Using Handlers instantiated with default constructor doesn't mean "code will run on UI thread" in general. By default, handlers binded to Thread from which they was instantiated from. So if you suspect that kind of an ANR use a handler – Charuක Jan 13 '17 at 11:05
2

Normally your code is executed on your UI thread. For longer taking tasks (such as network requests, etc...) you will use a background tasks (Handler, AsyncTask, Thread, ...).

As your Views can only be touched from a UI thread, you use runOnUiThread() if you are executing code in a background thread and you need to update your views, from this background thread.

Tobias
  • 7,282
  • 6
  • 63
  • 85
  • That means runOnUiThread run code in background? Can it create ANR? – Mohit Gaur Jan 13 '17 at 10:44
  • No, you call `runOnUiThread` **from** your backround thread to execute code on you main thread – Tobias Jan 13 '17 at 10:45
  • What if, i will use this....new Thread(new Runnable() { (At)Override public void run() { runOnUiThread(new Runnable() { (At)Override public void run() { //Code here } }); } }).start(); – Mohit Gaur Jan 13 '17 at 10:47
1

To explain 'why' Android has the 'runOnUiThread()' option, it is important to understand that java is only used to create the bytecode (dex) that Android uses. The code running on the phone is NOT java.

Additionally, Android threads 'can' have a thing called a 'looper'. This 'looper' is what handles 'tasks(technically runnables and messages)' in order via a queue. The 'main ui thread' has by default a looper already attached to it.

That means that your runnable you created was put onto the looper's queue of the main UI thread. (this is why the runnable is NOT instantaneously ran, but will be ran 'quickly'/'soon')

The reason you'd use a runnable to run code on the UI thread is because you are in some other 'background thread' that you created... and want to update the UI in some way. (Only the UI thread can interact with the UI)

mawalker
  • 2,072
  • 2
  • 22
  • 34