Volley 核心源码解析(二)

[size=large][color=orange]请求队列 RequestQueue[/color][/size]

每一个使用过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);
[color=red][b] synchronized (mCurrentRequests) {
mCurrentRequests.add(request);
}[/b][/color]

// 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.
[b][color=red] if (!request.shouldCache()) {
mNetworkQueue.add(request);
return request;
}[/color][/b]

// Insert request into stage if there's already a request with the same cache key in flight.
[b][color=red] synchronized (mWaitingRequests) {[/color][/b]
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);
[color=red][b] mWaitingRequests.put(cacheKey, stagedRequests);[/b][/color]
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.
[color=red][b] mWaitingRequests.put(cacheKey, null);
mCacheQueue.add(request);[/b][/color]
}
return request;
}
}

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

那么这两个是什么呢?
[color=brown] /**
* 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<?>>();[/color]

[color=brown]/**
* Staging area for requests that already have a duplicate request in flight.
*
* [list]
* <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 [i]not[/i] contained in that list. Is null if no requests are staged.</li>
* [/list]
*/
private final Map<String, Queue<Request<?>>> mWaitingRequests =
new HashMap<String, Queue<Request<?>>>();[/color]

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

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

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

[/color]


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

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

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

mNetworkQueue 的定义如下:

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

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

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

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

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


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


下一节 Volley 的任务调度模型 [url]http://f303153041.iteye.com/blog/2281352[/url]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值