Difference between android runOnUiThread and simple code in java

I am starting to develop applications for Android. I work with threads in android.I read about runOnUiThread , which runs the code on the main user interface (if I'm not mistaken, I think.).

My question is what is the difference between normal code on the main user interface and the 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 the difference in both examples. Please help me. Your answer will be new training for me.

+7
android multithreading android-runonuithread
source share
3 answers

Assuming you mean simple code for UIThread code,

What is a stream?

The thread determines the process that is running.

First runOnUiThread ..

Runs the specified action in the user interface thread . If the current thread is a UI thread, then the action is performed immediately. If the current thread is not a UI thread, the action is sent to the UI thread event queue.

What is UIThread?

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

    So simple Everything that causes the user interface to update or change needs to happen in the user interface thread

When you explicitly create a new thread to work in the background , this code does not run on UIThread.Now, what if you want to do something that changes the user interface? Then you can runOnUiThread

You must use runOnUiThread() if you want to update your user interface from a non-UI thread. For example, if you want to update your user interface from a background thread. You can also use the Handler for the same.

+12
source share

Typically, your code runs in a user interface thread. For longer tasks (for example, network requests, etc.) you will use background tasks (Handler, AsyncTask, Thread, ...).

Since your views can only be affected from the user interface thread, you use runOnUiThread() if you are executing code in the background thread and you need to update your views from this background thread.

+2
source share

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

In addition, Android threads can have a thing called a looper. This "looper" is what processes "tasks (technically started and messages)" in the order of the queue. The "main stream ui" is already attached to it by default.

This means that your executable file that you created has been put in the looper queue of the main UI thread. (this is why runnable is NOT executed instantly, but will be "fast" / "soon")

The reason you use runnable to run code in the user interface thread is because you are in some other "background thread" that you created ... and you want to somehow update the interface. (Only the user interface can interact with the user interface)

+1
source share

All Articles