`
f303153041
  • 浏览: 44851 次
社区版块
存档分类
最新评论

Volley 核心源码解析(二)

 
阅读更多
请求队列 RequestQueue

每一个使用过Volley的同行们都用过 RequestQueue.add(request) 这个方法,看看这个方法到底做了什么:

public <T> Request<T> add(Request<T> request) {
        // Tag the request as belonging to this queue and add it to the set of current requests.
        request.setRequestQueue(this);
        synchronized (mCurrentRequests) {
            mCurrentRequests.add(request);
        }


        // Process requests in the order they are added.
        request.setSequence(getSequenceNumber());
        request.addMarker("add-to-queue");

        // If the request is uncacheable, skip the cache queue and go straight to the network.
        if (!request.shouldCache()) {
            mNetworkQueue.add(request);
            return request;
        }


        // Insert request into stage if there's already a request with the same cache key in flight.
       synchronized (mWaitingRequests) {
            String cacheKey = request.getCacheKey();
            if (mWaitingRequests.containsKey(cacheKey)) {
                // There is already a request in flight. Queue up.
                Queue<Request<?>> stagedRequests = mWaitingRequests.get(cacheKey);
                if (stagedRequests == null) {
                    stagedRequests = new LinkedList<Request<?>>();
                }
                stagedRequests.add(request);
               mWaitingRequests.put(cacheKey, stagedRequests);
                if (VolleyLog.DEBUG) {
                    VolleyLog.v("Request for cacheKey=%s is in flight, putting on hold.", cacheKey);
                }
            } else {
                // Insert 'null' queue for this cacheKey, indicating there is now a request in
                // flight.
                mWaitingRequests.put(cacheKey, null);
                mCacheQueue.add(request);

            }
            return request;
        }
    }

我们看到  这里面有两个被用synchronized 块锁住的对象:mCurrentRequests,mWaitingRequests

那么这两个是什么呢?
    /**
     * The set of all requests currently being processed by this RequestQueue. A Request
     * will be in this set if it is waiting in any queue or currently being processed by
     * any dispatcher.
     */
    private final Set<Request<?>> mCurrentRequests = new HashSet<Request<?>>();


/**
     * Staging area for requests that already have a duplicate request in flight.
     *
     *

         *     <li>containsKey(cacheKey) indicates that there is a request in flight for the given cache
         *          key.</li>
         *     <li>get(cacheKey) returns waiting requests for the given cache key. The in flight request
         *          is not contained in that list. Is null if no requests are staged.</li>
         *

     */
    private final Map<String, Queue<Request<?>>> mWaitingRequests =
            new HashMap<String, Queue<Request<?>>>();


看英文注释意思:
mCurrentRequests 大意是,当前在这个队列里所有正在被执行的请求的集合。所有的正在被执行的 或者  在队列中等待执行的请求都应该放进这个set 集合中。

mWaitingRequests:包含重复请求的临时区域,

通俗的说就是 一个是所有正在执行和等待执行的请求集合,另一个是存放重复请求的临时区域 是一个map.




当我们调用add 方法的时候 首先是把请求 加入到 mCurrentRequests  中,当然咯,这个过程是同步的。
此时volley 还给每个请求设置了 序号 和 备注:
request.setSequence(getSequenceNumber());
request.addMarker("add-to-queue");


接下来如下:
if (!request.shouldCache()) {
            mNetworkQueue.add(request);
            return request;
        }

这段代码很有意思,如果一个请求不支持被缓存的话,马上就把这个请求交给mNetworkQueue
中去执行,后面的代码不再执行。

mNetworkQueue  的定义如下:

/** The queue of requests that are actually going out to the network. */
    private final PriorityBlockingQueue<Request<?>> mNetworkQueue =
        new PriorityBlockingQueue<Request<?>>();


这是一个全新的阻塞式的队列。这个队列的作用 后面再讲。

在Request 类中 我们看到
private boolean mShouldCache = true;
说明 Volley中默认每一个请求都是要被缓存的。

接下来 通过Request的cacheKey 区分请求是否重复,如果有重复请求,就把这个请求所属的队列拿出来,然后把新的请求加到队列中,再存到 mWaitingRequests中。
如果mWaitingRequests中没有这个请求,就用这个请求的 cacheKey 去保存一个NULL值,

言简意赅的说;每一个请求都会在执行的同时放到 mWaitingRequests中 ,同时也要被放进缓存队列中,
mCacheQueue:
private final PriorityBlockingQueue<Request<?>> mCacheQueue =
        new PriorityBlockingQueue<Request<?>>();



mCacheQueue 中的请求何时执行,后面会讲到。






下一节 Volley 的任务调度模型  http://f303153041.iteye.com/blog/2281352








0
1
分享到:
评论

相关推荐

    Volley源码解析

    Volley源码解析,个人的理解与注释

    volley源码和jar包

    这是volley的源码以及jar包,用于小数据量的频繁的网络请求。

    Volley框架源码

    关于Volley框架的源码,及一些关于开发的一些例子讲解

    Volley源码+包

    Volley源码+包:在开发android使用第三方框架有两种方法:一是直接加框架的源码,二是在libs中加jar包。

    android-volley源码

    android-volley源码代码 android Volley ---------- This is an unofficial mirror for [android volley library](https://android.googlesource.com/platform/frameworks/volley), the source code will ...

    volley源码

    volley源码,有兴趣的同学可以下载研究一下。 谷歌建议使用volley框架进行网络请求。

    volley联网解析网络上的xml文件

    采用volley的联网方式,实现解析服务端返回的xml数据

    google官方最新volley源码

    google官方最新volley源码,如果你想对volley有更深的认识就下来学习吧

    Volley源码与jar包

    Volley是2013年Google I/O上发布的,它是Android平台上的网络通信库,对常用的网络通信功能作了封装,能使网络通信更快, 更简单,更健壮。以前使用网络通信一般都是用AsyncTaskLoader、HttpURLConnection、...

    android volley源码

    android volley源码,从google下载的volley源码,方便理解和使用

    Github-volley jar包和源码

    volley是一个优秀的安卓开源网络访问工具 这里包含一个volley代码jar和源码,版本是2015.03.03的1.0.11版本 更多资料可以参见volley的github地址: https://github.com/mcxiaoke/android-volley

    Volley源码

    Volley源码,相关博文:http://www.cnblogs.com/tianzhijiexian/p/4255488.html

    Android Volley源码20160414版

    Android Volley源代码,更新日期20160414

    可查看Volley源码

    volley作为库文件 这个demo ,可以查看volley源码

    volley框架源码实例

    volley框架的实例源码,包含二次封装,可下载导入工程项目运行

    volley的demo(包括volley源码)

    一共两个demo: 1.使用volley.jar进行开发的demo 2.将volley源码添加到工程中,进行演示的demo。最重要的是在源码中添加了很多中文注释。

    Volley源码及文档

    Volley源码和文档,本人验证无误,遂上传,希望对你有帮助~

    Volley.jar及源码

    1.0.19版本的Volley.jar和Volley源码。截止于2015.09.08。更多详情见github:https://github.com/mcxiaoke/android-volley

Global site tag (gtag.js) - Google Analytics