Which thread does Runnable run on?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 02:53:47

问题


I want to update UI every 100ms. After searching in StackOverflow, I found a solution using Runnable and Handler like this

final Handler handler = new Handler();
Runnable runnable = new Runnable() {
    @Override
    public void run() {
        //update UI here

        handler.postDelayed(this, 100);
    }
};
runnable.run();

It works! But I have some questions:

  1. Which thread does this Runnable run on? MainThread or another thread? Here is the docs about postDelay

Handler is attached MainThread, so is Runnable running on MainThread?

  1. If Runnable is running on MainThread, why needs Handler? According to my knowledge, Handler is used to send messages between two threads

回答1:


Which thread does this Runnable run on?

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        //update UI here

        handler.postDelayed(this, 100);
    }
};
runnable.run()

This Runnable runs on the current thread, i.e. the thread that invokes this code. It doesn't magically create, or constitute, another thread. Runnable.run() is only a method call.

The subsequent executions of this thread, by the Handler, in whatever thread the Handler runs in, do essentially nothing except reschedule themselves. It's hard to believe this is a solution to anything.




回答2:


Handler is attached MainThread, so is Runnable running on MainThread?

From Handler documentation:

Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

If you want to run your Runnable on different Thread, you can use HandlerThread .

Related post:

Why use HandlerThread in Android

If Runnable is running on MainThread, why needs Handler? According to my knowledge, Handler is used to send messages between two threads

There are two main uses for a Handler:

  1. To schedule messages and runnables to be executed as some point in the future
  2. To enqueue an action to be performed on a different thread than your own.

If you are using only MainThread,Handler is useful to send message at some point of time in future. If you are using different Threads, Handler is useful to communicate between the threads.




回答3:


In your example, the Runnable runs on the UI Thread.

If you want your Handler and all its Runnable to run in a different Thread, you'll have to assign it a a new HandlerThread's Looper.

final HandlerThread handlerThread = new HandlerThread(MY_THREAD_ID);
handlerThread.start();
final Handler handler = new Handler(handlerThread.getLooper());

You can then pass the Runnable instance via postDelayed(Runnable, long).

Runnable r = new Runnable() {
    @Override public void run() {
        handler.postDelayed(this, 2000);
    }   
};

handler.postDelayed(r, 0);


来源:https://stackoverflow.com/questions/47670169/which-thread-does-runnable-run-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!