Volley爲什麼不用ThreadPoolExecutor

不少人看過Volley的源碼,會有一個困惑,執行網絡通訊操做的4個線程是用數組管理的,沒有用到ThreadPoolExecutor。java

貼代碼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方法:app

  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異步執行,主要是爲了提升性能,畢竟網絡請求的開銷基本上要大於消息隊列的處理。異步

相關文章
相關標籤/搜索