3

In following code
The way we are using runOnUiThread
shouldn't this create issue with the existing UI Thread
hence creating an issue with the application , hence shouldnt be used

Thread thread = new Thread(new Runnable(){
    @Override
    public void run(){
        //what is meant by the inside code of this run(), how is this updating the UI 
        runOnUiThread(new Runnable(){
            @Override
            public void run(){

            }
        })
    }
})
ShankPossible
  • 427
  • 4
  • 14
  • @TimCastelijns runOnUiThread is a system thread by Android , which is probably already running , how can i provide a new Runnable to the same , isnt that wrong – ShankPossible Jul 04 '18 at 09:48
  • @BrijeshJoshi Question is more focused on the workings of runOnUiThread rather than how to use it in our code , the part that is creating trouble is runOnUiThread is main thread or worker thread , and in case its UI thread , what happens to UI thread that was working before i have called this runnable, I have updated the Question to make it more clear – ShankPossible Jul 04 '18 at 10:09
  • 2
    @ShankPossible running a separate thread will create problem when you have to do operations on the UI. while using **runOnUIThread** You are forcing it to do the UI tasks on the mainThread only If you in detail of this method you'll find that out. – Brijesh Joshi Jul 04 '18 at 10:17

7 Answers7

3

In android,for long running task you should use separate thread such as AsyncTask() or service.Suppose you want to update your UI like you want to show any Toast to user then you should write runOnUiThread(),because only UI thread will allow to touch UI components.

    getActivity().runOnUiThread(new Runnable() 
    {

     @Override
     public void run() {              
     Toast.makeText(getContext(), "API calling done!!", Toast.LENGTH_LONG).show();

    }
  });
  • there is only one main thread , so if i am providing a new Runnable to it , shouldnt that create issue with existing UI Thread that was running before the code was called – ShankPossible Jul 04 '18 at 10:29
2

Whenever we have some Long running tasks we switch to some worker threads and avoid Main Thread and allow a smoother user experience and avoid ANR.

But, when the time comes to update the UI we must “return” to the Main Thread, as only Main Thread is allowed to touch and update the application UI.

we can achieve this by making a call to the Activity’s runOnUiThread() method:

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.

Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29
2

There are two types of thread in Android.

1 is UI or Main thread on which your UI elements (layouts) are rendered.

2 is Worker Thread in which long task should be executed (like AsyncTask & Networking).

If you write some task in new Thread, that mean that task will be executed in worker thread.

Now you will use runOnUiThread or new Handler(Looper.getMainLooper()) because you can not touch UI elements in worker thread.

So basically when you are updating UI like setText(), or Toast or any UI operations, you will have to UI thread and you should use worker thread when you are doing some long executions.

Edit

Generally we don't have to manage threading in Android. Because all libraries we use are smart. Although in some cases we have to manage threading as well.

Example

Assume you are calling an web-service(api) in a new Thread, now when response comes you want show a Toast. If you just write Toast.show... directly in response inside worker Thread you will get exception.

Only the original thread that created a view hierarchy can touch its views.

Now to overcome this issue you have to use runOnUiThread, so that you can show Toast.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • Thanks for replying, I understood the worker thread part and we are calling runOnUIThread to update it to the UI Thread , however the way its done ,its strange , shouldnt it create issue with something which is already running in background – ShankPossible Jul 04 '18 at 09:56
  • See edit part. Hope that will make you understand. – Khemraj Sharma Jul 04 '18 at 10:03
  • Thanks @Khemraj so runOnUiThread is a worker thread or UI main Thread ? – ShankPossible Jul 04 '18 at 10:07
  • runOnUiThread runs code in main or UI thread (both are same). – Khemraj Sharma Jul 04 '18 at 10:08
  • and there is only one main thread , so if i am providing a new Runnable to it , shouldnt that create issue with existing UI Thread that was running before the code was called – ShankPossible Jul 04 '18 at 10:27
  • the 'Runnable' action is posted to the event queue of the UI thread, and this event queue is managed by operating system. The case you are worrying about won't happen. – shizhen Jul 05 '18 at 01:25
0

When a new Thread is created and executed, it does the task in the background thread. But the method runOnUiThread() is used for running the code on the main UI thread.

In your code, runOnUiThread() method is called and hence you are able to update the UI thread from the other thread.

Mehul Kanzariya
  • 888
  • 3
  • 27
  • 58
0

when thread is running in the method runOnUiThread () will update the UI Components (textview .. etc..)

by calling runOnUiThread, you can update the status too

JustBaron
  • 2,319
  • 7
  • 25
  • 37
0

No worries! This is one of the standard way for updating UI components from a separate thread or even on UI thread itself on Android platform.

Also, runOnUiThread is an method of Activity class, it 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.

Another standard way is using Handler and Message as officially documented by Android Developer here https://developer.android.com/training/multiple-threads/communicate-ui

shizhen
  • 12,251
  • 9
  • 52
  • 88
0

shouldn't this create issue with the existing UI Thread

Not at all. You said "existing UI Thread". There is only one UI thread. The runOnUiThread() will only add the runnable to a queue of tasks which the UI thread executed one by one. You can check the doc.

Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67