Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between android runOnUiThread and simple code in java

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.

like image 616
Mohit Gaur Avatar asked Jan 13 '17 10:01

Mohit Gaur


People also ask

What is runOnUiThread in Java?

Android App Development for Beginners if user wants to add some extra operations on UI, it will get load and provides ANR. Using runOnUiThread going to do back ground operations on worker thread and update the result on main thread. This example demonstrate about How do we use runOnUiThread in Android.

How does runOnUiThread work?

Basically what runOnUiThread() will do is - Runs the specified action on the UI thread. It will check the current thread and if it finds its the MainThread it will execute that task immediately , otherwise first it will switch you to app MainThread and then it will execute the given task.

Why should you avoid to run non UI code on the main thread?

If you put long running work on the UI thread, you can get ANR errors. If you have multiple threads and put long running work on the non-UI threads, those non-UI threads can't inform the user of what is happening.

How do you use kotlin runOnUiThread?

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.


1 Answers

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.

like image 106
Charuක Avatar answered Oct 23 '22 02:10

Charuක