使用RoboSpice执行Parellel(Parellel execution with RoboSpice)

编程入门 行业动态 更新时间:2024-10-27 15:24:32
使用RoboSpice执行Parellel(Parellel execution with RoboSpice)

通过RoboSpice异步执行大量请求的最简单方法是什么?

我在某处读到了我需要实现一个RequestRunner但我不知道将它与SpiceManager合并的方式,任何想法?

What's the easiest way to execute a bunch of requests asynchronously via RoboSpice?

I read somewhere that I need to implement a RequestRunner but I don't know the way to merge it with SpiceManager, Any ideas?

最满意答案

您可以覆盖可用线程的数量,定义您自己的自定义SpiceService:

public class CustomSpiceService extends RetrofitGsonSpiceService { /** * Overrides the number of threads that will be used to make requests. The default * is 1. */ @Override public int getThreadCount(){ return NUM_THREAD; } }

之后,您可以在经理中使用新的spiceService:

private SpiceManager spiceManager = new SpiceManager(CustomSpiceService.class);

作为奖励,您可以检测连接的类型,因此如果您使用的是Wifi连接,则可以拥有更多线程。

/** * Overrides the number of threads that will be used to make requests. The default * is 1, so if we are on a fast connection we use 4, otherwise we use 2. */ @Override public int getThreadCount() { ConnectivityManager connectivityManager = (ConnectivityManager) DaftApp.getInstance().getSystemService(CONNECTIVITY_SERVICE); NetworkInfo info = connectivityManager.getActiveNetworkInfo(); if(info==null){ return 2; // there is no network available now. Anyway we use the default num of thread } switch (info.getType()) { case ConnectivityManager.TYPE_WIFI: case ConnectivityManager.TYPE_WIMAX: case ConnectivityManager.TYPE_ETHERNET: return 4; case ConnectivityManager.TYPE_MOBILE: return 2; default: return 2; } }

You can override the num of thread available, defining your own custom SpiceService:

public class CustomSpiceService extends RetrofitGsonSpiceService { /** * Overrides the number of threads that will be used to make requests. The default * is 1. */ @Override public int getThreadCount(){ return NUM_THREAD; } }

After that you can use your new spiceService in you manager:

private SpiceManager spiceManager = new SpiceManager(CustomSpiceService.class);

As a bonus, you can detect the the type of your connection, so you can have more threads if you are in a Wifi connection.

/** * Overrides the number of threads that will be used to make requests. The default * is 1, so if we are on a fast connection we use 4, otherwise we use 2. */ @Override public int getThreadCount() { ConnectivityManager connectivityManager = (ConnectivityManager) DaftApp.getInstance().getSystemService(CONNECTIVITY_SERVICE); NetworkInfo info = connectivityManager.getActiveNetworkInfo(); if(info==null){ return 2; // there is no network available now. Anyway we use the default num of thread } switch (info.getType()) { case ConnectivityManager.TYPE_WIFI: case ConnectivityManager.TYPE_WIMAX: case ConnectivityManager.TYPE_ETHERNET: return 4; case ConnectivityManager.TYPE_MOBILE: return 2; default: return 2; } }

更多推荐

本文发布于:2023-07-26 03:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1270676.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:RoboSpice   Parellel   execution

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!