安卓创建一个新的异步任务的优先级高于其他人的背景或Dropbox的核心API问题优先级、高于、创建一个、其他人

2023-09-03 22:08:24 作者:烟尘锁寒秋

我从Dropbox的加载多张照片使用Android的核心API,使请求的时候都序列化。

I am loading multiple photos from Dropbox using the Core API in Android and when making requests they are serialized.

我收到缩略图网格视图显示,当用户点击其中一个,它进入另一个活动它将从服务器的完整清晰度的版本。

I am getting the thumbnails to show in a grid view and when the user clicks on one of them it goes to another activity it fetches the full res version from the server.

它这样做是好的,但在高分辨率的版本只启动时所有其他异步任务获取的缩略图完成后进行下载。

It does this fine, but the high res version only starts to be downloaded when all the others async tasks fetching the thumbnails are finished.

所以,我想知道的是,这是Dropbox的核心API的限制?还是有办法让高分辨率异步任务的优先级高于其他人,以便它立即下载,然后其他人可以重新开始。

So what I want to know is, is this a limitation of the Dropbox Core API? Or is there a way to make the high res async task have priority over others so it is immediately downloaded and then the others can resume.

推荐答案

这是因为如何异步任务的实际工作。他们没有在3.0+并行运行(他们曾经到3.0),因为太多的新手开发者不能够并行程序没有错误,因此谷歌决定改变它。相反,异步任务上FIFO顺序一个线程中运行。

This is due to how async tasks actually work. They don't run in parallel on 3.0+ (they used to until 3.0), because too many newbie developers weren't able to program in parallel without errors, so Google decided to change it. Instead, async tasks run on a single thread in FIFO order.

不过,您可以覆盖此。而不是调用asynctask.execute()的调用asynctask.executeOnExecutor()并使用THREAD_POOL_EXECUTOR。这将在自己的线程执行它并行。我相信有一个线程上限,但它至少会做出一些并行运行。

You can override this however. Instead of calling asynctask.execute(), call asynctask.executeOnExecutor() and use a THREAD_POOL_EXECUTOR. This will execute it in parallel on its own thread. I believe there's a thread cap, but it will at least make several run in parallel.

如果该线程帽成为一个问题,你总是可以下降到使用线程,而不是使用异步任务。你需要做一些工作自己做一个onPostExecute,但它并不难。如果你正在creeating你自己的线程可以弥补到操作系统的限制。

If the thread cap becomes an issue, you can always drop down to using threads instead of using async tasks. You need to do some work yourself to do an onPostExecute, but it isn't that hard. And if you're creeating your own threads you can make up to the OS limit.