Android,线程 - 处理程序/异步任务

发布于 2024-11-26 04:10:35 字数 409 浏览 1 评论 0原文

我是一个通过书本学习 Android 的菜鸟,对于线程,我看到书中解释了 3 种主要方法:

  • 通过消息传递的处理程序
  • 通过 post
  • Asynctask 的

处理程序所有 3 种似乎都不错,我的问题是:
使用上述其中一项有特定时间吗?还是个人喜好?

编辑:
如果不是个人偏好,请给我一个例子,说明何时使用其中一个(如果不是另一个)(即使是链接也将不胜感激)

因为我计划运行多个线程(例如,一个用于总应用程序运行时间,一个用于总应用程序运行时间)在应用程序中选择答案之前的时间,用于移动背景图像等的时间)并想知道选择哪一个。我可以使用这 3 种方法中的任何一种来实现我想要的功能,所以我很困惑为什么有 3 种方法,而它可以用任何一种来完成。

谢谢!

Am a noob learning Android via a book and for threading I see there are 3 main ways that are explained in the book:

  • Handlers via messaging
  • Handlers via post
  • Asynctask

All 3 seem good, my question is:
Is there a specific time when you use one of the above? Or is it personal preference?

EDIT:
If its not personal preference, please give me an example as to when you would use one if not the other (even a link would be appreciated)

Because I am planning on having multiple threads running (eg, one for total app running time, one for time before answer is selected etc in the app, one for moving the background image etc) and want to know which to choose. I can do the functionality of what I want using either of these 3 methods so am confused as to why there are 3 when it can be done with either one.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

得不到的就毁灭 2024-12-03 04:10:36

处理程序:绑定到创建它的线程。 Queue/Enqueue 消息队列中的任务。当它将一条消息放入队列时,它会将其传递给另一个线程,而不是创建它的线程。将其视为线程的代理。它负责处理发送给它的请求并相应地委派它们。

发布/消息传递:如上所述,它只是处理程序发布(排队)或消息传递(排队)。

Asynctask:是一个线程,主要用于需要在后台完成的小任务,并根据其进度更新 UI(想想加载栏或下载;UI 随着该进度更新)。

Handler: Bound to the thread that created it. Queue/Enqueue's tasks in the message queue. When it enqueues a message, it hands it to another thread than the one that created it. Think of it as an agent to the thread. It takes care of requests sent to it and delegates them accordingly.

Post/Messaging: As explained above, it's just the handler posting (queueing) or messaging (enqueueing).

Asynctask: Is a thread that mainly is for small tasks that need to be done in the background and update the UI dependent on their progress (think loading bar, or a download; UI being updated with that progress).

背叛残局 2024-12-03 04:10:36

看看 Needle,你就可以忘记处理程序和异步任务了。 Needle 是一个开源、简单但功能强大的 Android 多线程库。有了它,你可以说这样的话:

Needle.onMainThread().execute(new Runnable() {
    @Override
    public void run() {
        // e.g. change one of the views
    }
});

或者

Needle.onBackgroundThread().execute(new UiRelatedTask<Integer>() {
    @Override
    protected Integer doWork() {
        int result = 1+2;
        return result;
    }

    @Override
    protected void thenDoUiRelatedWork(Integer result) {
        mSomeTextView.setText("result: " + result);
    }
});
  • 非常简单的 API
  • 固定线程池大小
  • 可定制的线程池大小
  • 支持 UI 交互(“做工作,然后在 UI 线程上使用结果”)
  • android 1.5+
  • 在所有平台版本上的行为相同

检查一下GitHub:https://github.com/ZsoltSafrany/needle

Check out Needle and you can forget Handlers and Asynctasks. Needle is an open-source, simple but powerful multithreading library for Android. With it you can say things like:

Needle.onMainThread().execute(new Runnable() {
    @Override
    public void run() {
        // e.g. change one of the views
    }
});

or

Needle.onBackgroundThread().execute(new UiRelatedTask<Integer>() {
    @Override
    protected Integer doWork() {
        int result = 1+2;
        return result;
    }

    @Override
    protected void thenDoUiRelatedWork(Integer result) {
        mSomeTextView.setText("result: " + result);
    }
});
  • very simple API
  • fixed thread pool size
  • customizable thread pool size
  • supports UI interaction ("do work and then use result on UI thread")
  • android 1.5+
  • behaves the same on all platform versions

Check it out on GitHub: https://github.com/ZsoltSafrany/needle

凉薄对峙 2024-12-03 04:10:35

Handler 是一种非常基本的方法,允许您在不同的线程(通常是 UI 线程)上执行一些代码。它不会告诉您如何运行线程,您可以在这方面自由地做任何您想做的事情。如果您有显式消息传递,我会选择 handleMessage 方法。也就是说,您需要发送和接收一些数据。如果你只需要做一些事情,你可以使用Runnable。然而,这两种方法都是可用的,并且通常会根据偏好进行选择。

AsyncTask 是在幕后使用处理程序的更高级别概念。如果您使用它,您将不必自己处理线程。您的异步代码将通过线程池执行并由框架控制。您将有两个方法允许您在 UI 线程上执行代码(onPostExecuteonProgressUpdate)。

简而言之,对于使用多个线程执行的大多数操作,选择 AsyncTask 将使您无需自己处理线程管理。如果您必须拥有自己的线程,则使用处理程序。但请注意,在其他情况下您必须使用处理程序。例如,使用 Messenger。我也遇到过使用 onProgressUpdate 不够的情况。在这种情况下,我也会使用 doInBackground 中的处理程序。

Handler is a very basic way that allows you to execute some code on a different thread, typically UI thread. It does not tell you how to run your threads and you are free to do whatever you want in that respect. I would choose handleMessage approach if you have explicit messaging. That is you need to have some data sent and received. If you just need to do something, you can use Runnable. However, both methods are usable and a choice often would be result of a preference.

AsyncTask is higher level concept that uses handlers under the hood. If you use it, you will not have to deal with threads yourself. Your asynchronous code will be executed via thread pool and controlled by the framework. You will have two methods that allow you to execute code on UI thread (onPostExecute and onProgressUpdate).

In a nutshell, choose AsyncTask for most things that you do with multiple threads it will absolve you of a need to deal with thread management yourself. If you must have your own threads only then use handlers. Note however, that there are other cases when you have to use handlers. For example, with Messenger. Also I had cases when using onProgressUpdate is not sufficient. In this case I would use handler as well from doInBackground.

Oo萌小芽oO 2024-12-03 04:10:35

AsynTasks 可能最适合复杂的任务,例如当您需要大量处理和大量消息传递时。您也可以通过处理程序来实现它们,但就个人而言,这样您的代码会变得有点混乱。

AsynTasks are probably the best fit for complex tasks, like when you would need a lot of processing and lot of message passing. You could achieve them through Handlers as well, but personally speaking, you code gets a little messy that way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文