标签:
很多人看过Volley的源码,会有一个困惑,执行网络通信操作的4个线程是用数组管理的,没有用到ThreadPoolExecutor。
贴代码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();
}
}
NetworkDispather的实例直接用数组管理。NetworkDispather是网络请求的线程。
那为何不用ThreadPoolExecutor。我个人觉得可能是这样的,ThreadPoolExecutor的使用,目的是为了线程的重用,提升效率。但看过Volley中的网络线程NetworkDispatcher的源代码,就知道了在这个场景不适用ThreadPoolExecutor,贴NetworkDispatcher的源代码,主要是run方法:
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
Request<?> request;
while (true) {
try {
// Take a request from the queue.
request = mQueue.take();
} catch (InterruptedException e) {
// We may have been interrupted because it was time to quit.
if (mQuit) {
return;
}
continue;
}
try {
request.addMarker("network-queue-take");
// If the request was cancelled already, do not perform the
// network request.
if (request.isCanceled()) {
request.finish("network-discard-cancelled");
continue;
}
addTrafficStatsTag(request);
// Perform the network request.
NetworkResponse networkResponse = mNetwork.performRequest(request);
request.addMarker("network-http-complete");
// If the server returned 304 AND we delivered a response already,
// we‘re done -- don‘t deliver a second identical response.
if (networkResponse.notModified && request.hasHadResponseDelivered()) {
request.finish("not-modified");
continue;
}
// Parse the response here on the worker thread.
Response<?> response = request.parseNetworkResponse(networkResponse);
request.addMarker("network-parse-complete");
// Write to cache if applicable.
// TODO: Only update cache metadata instead of entire record for 304s.
if (request.shouldCache() && response.cacheEntry != null) {
mCache.put(request.getCacheKey(), response.cacheEntry);
request.addMarker("network-cache-written");
}
// Post the response back.
request.markDelivered();
mDelivery.postResponse(request, response);
} catch (VolleyError volleyError) {
parseAndDeliverNetworkError(request, volleyError);
} catch (Exception e) {
VolleyLog.e(e, "Unhandled exception %s", e.toString());
mDelivery.postError(request, new VolleyError(e));
}
}
}
可以看到run方法里就是个while(true),设计这个线程就是为了线程存活着,循环执行请求,如果队列里没有数据,线程则会一直阻塞在mQueue.take(),这个设计思想和Android的消息队列类似,消息队列核心也是通过一个死循环取队列的数据,然后执行操作。根据ThreadPoolExecutor的使用场景和代码分析来看,从而得出结论,Volley不需要线程池技术。其实就一个NetworkDispatcher也够用,设计成4个NetworkDispatcher异步执行,主要是为了提高性能,毕竟网络请求的开销基本上要大于消息队列的处理。
标签:
原文地址:http://my.oschina.net/u/1463920/blog/493621