与改造并行的HTTP请求HTTP

2023-09-06 00:39:46 作者:深碍

我当前正在使用排球库,使网络请求,并显示下载的图像与 NetworkImageView Android应用程序

I have an Android application that is currently using Volley library to make network requests and show downloaded images with NetworkImageView.

我想考改造的能力,因为我需要运行大量的请求(千)我是有点担心的并行执行。 排球处理与并行请求的请求队列限制并发运行的请求到四个,而其他请求被排队的等待被执行。在改造单证我没有找到任何方式来处理的并发请求数,我怀疑这些细节都留给了这个库的开发。

I would like to test Retrofit's capabilities and since I need to run lots of requests (thousands) I'm a bit concerned about the parallel execution. Volley handles parallel requests with the RequestQueue that limits the concurrent running requests to four, while the other requests are enqueued waiting to be executed. In Retrofit documentations I haven't found any way to handle the number of concurrent requests and I suspect that such details are left to the developer in this library.

这是正确的?如果是这样,没有任何的android为本实施/库可用?否则,有什么处理并行请求的最佳实践?

Is this correct? If so, is there any android-oriented implementation/library available? Otherwise, what are the best practices to handle parallel requests?

推荐答案

改造使用了执行程序排队的请求。

Retrofit uses an Executor for queueing requests.

默认使用 Executors.newCachedThreadPool ,允许无限制线程。这符合大多数用例,因为通常你会永远只能有一个或两个请求发生一次。

The default uses Executors.newCachedThreadPool which allows for unlimited threads. This fits most use cases since normally you would only ever have one or two requests happening at once.

您可以通过构建 RestAdapter 时,提供自己的,但改变这种行为。呼叫setExecutors并通过在使用密闭线程池遗嘱执行人(仅限于你的任何号码等等)。对于第二个参数,只需通过的新实例MainThreadExecutor这样的回调发生在主线程中。

You can change this behavior, however, by supplying your own when building the RestAdapter. Call setExecutors and pass in an executor that uses a confined thread pool (limited to whatever number you would like). For the second argument, simply pass a new instance of MainThreadExecutor so that callbacks happen on the main thread.

 
精彩推荐