先看官方给个注释
`/**
- A request dispatch queue with a thread pool of dispatchers.
- Calling {@link #add(Request)} will enqueue the given Request for dispatch,
- resolving from either cache or network on a worker thread, and then delivering
- a parsed response on the main thread.
*/`
大概意思就是将会对请求进行分发,将会在cache或者network进行请求 并且在worker thread 进行处理,然后返回主线程一个a parsed response
RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
通过上面这条语句创建了一个RequestQueue对象 传过来一个DiskBasedCache对象 和BasicNetwork 对象。作用如下
/** Cache interface for retrieving and storing responses. */
private final Cache mCache;
/** Network interface for performing requests. */
private final Network mNetwork;
经过2个重载的构造方法后,在下面的代码中完成了初始化
public RequestQueue(Cache cache, Network network, int threadPoolSize,
ResponseDelivery delivery) {
mCache = cache;
mNetwork = network;
mDispatchers = new NetworkDispatcher[threadPoolSize];
mDelivery = delivery;
}
mDispatchers 是一个大小为4的NetworkDispatcher 数组 ,而NetworkDispatcher是用来进行请求分发的线程。 mDelivery 是用来回传响应和错误的接口,ExecutorDelivery是其实现类。 这样RequestQueue 完成了构造。 在Volley类中调用RequestQueue 的start方法后执行如下代码:
public void start() {
stop(); // Make sure any currently running dispatchers are stopped.
// Create the cache dispatcher and start it.
mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);
mCacheDispatcher.start();
// Create network dispatchers (and corresponding threads) up to the pool size.
for (int i = 0; i < mDispatchers.length; i++) {
NetworkDispatcher networkDispatcher = new NetworkDispatcher(mNetworkQueue, mNetwork,
mCache, mDelivery);
mDispatchers[i] = networkDispatcher;
networkDispatcher.start();
}
}
stop()方法停止 CacheDispatcher ,NetworkDispatcher的分发。 然后新建一个CacheDispatche 对象,将参数传递过去完成构造。 mCacheQueue ,mNetworkQueue 为真正的请求队列。。。只不过一个是缓存分发一个是网络分发的。 PriorityBlockingQueue是带优先级的无界阻塞队列,每次出队都返回优先级最高的元素 。可以看http://ifeve.com/java-priorityblockingqueu/ 这位大神的博客讲解。。很厉害。
/** The cache triage queue. */
private final PriorityBlockingQueue<Request<?>> mCacheQueue =
new PriorityBlockingQueue<>();
/** The queue of requests that are actually going out to the network. */
private final PriorityBlockingQueue<Request<?>> mNetworkQueue =
new PriorityBlockingQueue<>();
CacheDispatche 和 NetworkDispatcher 都继承了Thread对象。调用其start方法以后将会执行其run()中的方法。
在使用volley时 调用RequestQueue 的start方法,将请求加入队列方法如下
/**
* Adds a Request to the dispatch queue.
* @param request The request to service
* @return The passed-in 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); //add to set
}
// Process requests in the order they are added.
request.setSequence(getSequenceNumber());
request.addMarker("add-to-queue");
// 如果不能缓存,则将请求添加到网络请求队列中.
if (!request.shouldCache()) {
mNetworkQueue.add(request);
return request;
}
mCacheQueue.add(request);
return request;
}
mCurrentRequests 是一个Set集合用来进行请求管理的 。注释如下:
/**
* 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<?>>();
RequestQueue 的主要方法就是上面这些了。如有不正确的地方,欢迎指正。