Android 在后台或 Dropbox Core API 问题中使新的异步任务优先于其他任务 [英] Android Make a new Async task have priority over others in background or Dropbox Core API issue

查看:11
本文介绍了Android 在后台或 Dropbox Core API 问题中使新的异步任务优先于其他任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Android 中的核心 API 从 Dropbox 加载多张照片,并在发出请求时将它们序列化.

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 Core 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 之前一直这样做),因为太多的新手开发人员无法在没有错误的情况下并行编程,因此 Google 决定对其进行更改.相反,异步任务按 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,但这并不难.如果您要创建自己的线程,则可以弥补操作系统限制.

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.

这篇关于Android 在后台或 Dropbox Core API 问题中使新的异步任务优先于其他任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆