何时使用handler.post()以及何时使用新线程()

时间:2021-04-03 17:28:27

I'm wondering when should I use handler.post(runnable); and when should I use new Thread(runnable).start();

我想知道我什么时候应该使用handler.post(runnable);什么时候应该使用新的Thread(runnable).start();

It is mentioned in developers documentation for Handler:

在Handler的开发人员文档中提到:

Causes the Runnable r to be added to the message queue. The runnable will be run on the thread to which this handler is attached.

导致Runnable r添加到消息队列中。 runnable将在连接此处理程序的线程上运行。

Does this mean if I write in the onCreate() of Activity class:

如果我在Activity类的onCreate()中写入,这是否意味着:

Handler handler = new Handler();
handler.post(runnable);

then runnable will be called in a separate thread or in the Activity's thread?

然后runnable将在一个单独的线程或Activity的线程中调用?

4 个解决方案

#1


71  

You should use Handler.post() whenever you want to do operations in the UI thread.

每当要在UI线程中执行操作时,都应该使用Handler.post()。

So let's say in the callback (which is running in separate thread) you want to change a TextView's text, you should use Handler.post().

所以让我们说回调(在单独的线程中运行)你想要改变TextView的文本,你应该使用Handler.post()。

In Android, as in many other UI frameworks, UI elements (widgets) can be only modified from main thread.

在Android中,与许多其他UI框架一样,UI元素(窗口小部件)只能从主线程修改。


Edit: the example of long-running tasks

编辑:长时间运行任务的示例

mHandler = new Handler();

new Thread(new Runnable() {
  @Override
  public void run () {
    // Perform long-running task here
    // (like audio buffering).
    // you may want to update some progress
    // bar every second, so use handler:
    mHandler.post(new Runnable() {
     @Override
     public void run () {
       // make operation on UI - on example
       // on progress bar.
     }
    });
  }
}).start();

Of course, if the task you want to perform is really long and there is a risk that user might switch to some another app in the meantime, you should consider using a Service.

当然,如果您要执行的任务非常长,并且在此期间存在用户可能切换到其他应用程序的风险,则应考虑使用服务。

#2


19  

To answer you specific question:

为了回答你的具体问题:

Does this mean if in the onCreate of Activity class I write:

这是否意味着如果在我写的onCreate of Activity类中:

Handler handler = new Handler() hanlder.post(runnable); then, runnable will be called in a separate thread or on the Activity's thread?

处理程序处理程序= new Handler()hanlder.post(runnable);那么,runnable将在一个单独的线程中调用还是在Activity的线程上调用?

No it won't be. The Runnable will be called on the Main Thread itself. Handler is simply used for posting a message to the thread to which it is attached (where its is created). It does not create a thread on its own. In your example, you created a Handler in the main Thread (that where Activity.OnCreate() is called) and hence any message posted on such a Handler will be run on the Main Thread only.

不,它不会。 Runnable将在主线程本身上调用。 Handler仅用于将消息发布到它所附加的线程(创建它的位置)。它不会自己创建一个线程。在您的示例中,您在主线程(调用Activity.OnCreate()的位置)中创建了一个Handler,因此在此类Handler上发布的任何消息都将仅在主线程上运行。

#3


11  

Example is jacked:

举例:

mHandler = new Handler();
new Thread(new Runnable(){
  @Override
  public void run () {
    mHandler.post(new Runnable() {
     @Override
     public void run () {
       mUiView.setX(x);
     }
    });
  }
}).start();

Alternatively you can skip the handler and use the post method on the view directly:

或者,您可以跳过处理程序并直接在视图上使用post方法:

new Thread(new Runnable(){
  @Override
  public void run () {
    mUiView.post(new Runnable() {
     @Override
     public void run () {
       mUiView.setX(x);
     }
    });
  }
}).start();

This is a good post that outlines the difference: What exactly does the post method do?

这是一个很好的帖子,概述了差异:post方法究竟做了什么?

#4


3  

use handler.post() when you want to post the code (usually from background thread) to the main thread. Yea, POST,just like you, post a letter to someone. With the help of handler the code will be executed ASAP i.e. almost immediately.

当你想将代码(通常是从后台线程)发布到主线程时,使用handler.post()。是的,POST,就像你一样,给某人发一封信。在处理程序的帮助下,代码将尽快执行,即几乎立即执行。

#1


71  

You should use Handler.post() whenever you want to do operations in the UI thread.

每当要在UI线程中执行操作时,都应该使用Handler.post()。

So let's say in the callback (which is running in separate thread) you want to change a TextView's text, you should use Handler.post().

所以让我们说回调(在单独的线程中运行)你想要改变TextView的文本,你应该使用Handler.post()。

In Android, as in many other UI frameworks, UI elements (widgets) can be only modified from main thread.

在Android中,与许多其他UI框架一样,UI元素(窗口小部件)只能从主线程修改。


Edit: the example of long-running tasks

编辑:长时间运行任务的示例

mHandler = new Handler();

new Thread(new Runnable() {
  @Override
  public void run () {
    // Perform long-running task here
    // (like audio buffering).
    // you may want to update some progress
    // bar every second, so use handler:
    mHandler.post(new Runnable() {
     @Override
     public void run () {
       // make operation on UI - on example
       // on progress bar.
     }
    });
  }
}).start();

Of course, if the task you want to perform is really long and there is a risk that user might switch to some another app in the meantime, you should consider using a Service.

当然,如果您要执行的任务非常长,并且在此期间存在用户可能切换到其他应用程序的风险,则应考虑使用服务。

#2


19  

To answer you specific question:

为了回答你的具体问题:

Does this mean if in the onCreate of Activity class I write:

这是否意味着如果在我写的onCreate of Activity类中:

Handler handler = new Handler() hanlder.post(runnable); then, runnable will be called in a separate thread or on the Activity's thread?

处理程序处理程序= new Handler()hanlder.post(runnable);那么,runnable将在一个单独的线程中调用还是在Activity的线程上调用?

No it won't be. The Runnable will be called on the Main Thread itself. Handler is simply used for posting a message to the thread to which it is attached (where its is created). It does not create a thread on its own. In your example, you created a Handler in the main Thread (that where Activity.OnCreate() is called) and hence any message posted on such a Handler will be run on the Main Thread only.

不,它不会。 Runnable将在主线程本身上调用。 Handler仅用于将消息发布到它所附加的线程(创建它的位置)。它不会自己创建一个线程。在您的示例中,您在主线程(调用Activity.OnCreate()的位置)中创建了一个Handler,因此在此类Handler上发布的任何消息都将仅在主线程上运行。

#3


11  

Example is jacked:

举例:

mHandler = new Handler();
new Thread(new Runnable(){
  @Override
  public void run () {
    mHandler.post(new Runnable() {
     @Override
     public void run () {
       mUiView.setX(x);
     }
    });
  }
}).start();

Alternatively you can skip the handler and use the post method on the view directly:

或者,您可以跳过处理程序并直接在视图上使用post方法:

new Thread(new Runnable(){
  @Override
  public void run () {
    mUiView.post(new Runnable() {
     @Override
     public void run () {
       mUiView.setX(x);
     }
    });
  }
}).start();

This is a good post that outlines the difference: What exactly does the post method do?

这是一个很好的帖子,概述了差异:post方法究竟做了什么?

#4


3  

use handler.post() when you want to post the code (usually from background thread) to the main thread. Yea, POST,just like you, post a letter to someone. With the help of handler the code will be executed ASAP i.e. almost immediately.

当你想将代码(通常是从后台线程)发布到主线程时,使用handler.post()。是的,POST,就像你一样,给某人发一封信。在处理程序的帮助下,代码将尽快执行,即几乎立即执行。