android - 如何在 Android 工作线程上对任务进行排队?

标签 android multithreading queue

我正在尝试找到一些简单的方法来在非 UI 线程的线程上对任务进行排队。

我有一个 BroadcastReceiver,每次拍摄图像时都会响应 Intent,并在每次发生这种情况时通知我的服务。该服务必须进行一些处理(缩放和旋转图像),我想在新线程上执行此操作(该服务与我的主 Activity 在同一线程上运行)。所以我一直在寻找 AsyncTask 。

我的问题是,如果在完全处理前一张图像之前拍摄下一张图像,我将同时运行两个 AsyncTasks,而我不希望这样。我想将下一个处理排队,直到第一个处理完成。

所以基本上我想要两个线程。一个是标准的 UI 线程,另一个是我的工作线程,我可以在其中执行一个任务队列。

最好的方法是什么?

最佳答案

I have a BroadcastReceiver that responds to Intents each time an image is taken, and notifies my Service each time that happens. The Service will have to do some processing (scaling and rotating an image) and I would like to do this on a new thread (The service is running on the same thread as my main Activity). So I've been looking at AsyncTask for that.

IntentService 是解决此类问题的一个很好的解决方案,因为它将工作队列与 Service 中的后台线程结合在一起。您只需重写 onHandleIntent() 即可完成实际工作。

My problem is that if the next image is taken BEFORE the previous has been fully processed, I will have two AsyncTasks running simultaneously, and I don't want that. I would like to queue the processing of the next one until the first one is done.

IntentService 一次只会处理一个 Intent

关于android - 如何在 Android 工作线程上对任务进行排队?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4058229/

相关文章:

java - 在 Java Swing GUI 中让线程不断运行和更新文本框的最佳方法是什么

java - 线程池中要创建多少个线程

java - Android - 如何找到两点之间的距离 - Java代码?

android - 获取当前运行我的应用程序的手机的 Android API 级别

android - 共享元素从 viewPager 中的 recyclerView 过渡到 fragment

java - 使 Java PriorityQueue 成为稳定的优先级队列

java - 可以仅使用队列将中缀表示法中的字符串转换为前缀表示法吗? (考虑唯一的操作是 + 和 - 的情况)

android - Cordova 文件传输下载 http 状态 401 - 仅限 ANDROID

java - 在 Java 1.6 中使用 For 循环和多线程

c# - .NET 中 UniqueQueue 和 UniqueReplacementQueue 集合的最高效实现