0

I have noticed that the main looper is called during animations, skipping over some animations if the ui thread is doing something labor intensive. One way of solving this problem is to run a thread or runnable or callable and then they join on the ui thread (automatically for callable, and manually by invoking the run on ui thread).

But does that mean that the ui thread may be interrupted during something important by the other threads? Ie if the other thread returns some data but the ui is in the middle of doing something with the old data.

Short answer: The main thread will finish what it is doing and will not be interupted, the other thread will post a runnable with the new data on the main thread and continue doing other stuff (if there are any), eventually the main thread will run the runnable containing the new data from the other thread.

Then Enok
  • 593
  • 5
  • 16

2 Answers2

0

runOnUiThread() works as follows

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.

So, to understand it by using the scenario you posted it would work something like, the main thread will execute the current event that it is working with and will add your event to the event queue of UI thread.

karan
  • 8,637
  • 3
  • 41
  • 78
0

Try this:-

runOnUiThread (new Thread(new Runnable() {  
        public void run() {
            while(i++ < 1000){
                btn.setText("#"+i);
                try {
                    Thread.sleep(300);
                } 
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
         }
    }));
Vicky
  • 19
  • 3