Volley庫源碼分析,volley庫源碼

來源:互聯網
上載者:User

Volley庫源碼分析,volley庫源碼
Volley使用了線程池來作為基礎結構,主要分為主線程,cache線程和network線程。
主線程和cache線程都只有一個,而NetworkDispatcher線程可以有多個,這樣能解決比並行問題。如:

其中左下角是NetworkDispatcher線程,大致步驟是:1.不斷從請求隊列中取出請求

request = mQueue.take();
2.發起網路請求
NetworkResponse networkResponse = mNetwork.performRequest(request);
3.把這個networkResponse轉化為期望的資料類型,比如Response<String>,Response<Json>,Response<Bitmap>
Response<?> response = request.parseNetworkResponse(networkResponse);
4.將網路響應加入緩衝
mCache.put(request.getCacheKey(), response.cacheEntry);

下面是NetworkDispatcher線程的主要代碼:
@Override    public void run() {        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);        Request request;        while (true) {            try {                // Take a request from the queue.                request = mQueue.take();//1.從請求隊列中取出一個網路請求,mQueue是BlockingQueue<Request>的實作類別            } 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;                }                // Tag the request (if API >= 14)                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {                    TrafficStats.setThreadStatsTag(request.getTrafficStatsTag());                }                // Perform the network request.                NetworkResponse networkResponse = mNetwork.performRequest(request);//2.發起網路請求                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);//3.把這個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);//4.將網路響應加入緩衝                    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));            }        }    }

要理解Volley的並行性實現,必需理解PriorityBlockingQueue並發類。注意到我們在NetworkDispatcher迴圈中並沒有使用顯式的同步(使用Lock或者使用synchronize),因為PriorityBlockingQueue的實現是安全執行緒的。在使用顯式的wait()和notifyAll()時存在的類和類之間的耦合可以因此消除,因為每個類只和BlockingQueue通訊。這個知識點可以參考《Java編程思想》P713. BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control. 
所以可以有多個NetworkDispatcher線程同時從請求隊列PriorityBlockingQueue中取出請求而不會產生線程衝突,那就是Volley支援多線程下載圖片的方式。
實際上,BlockingQueue介面是用於解決生產者-消費者問題的。
class Producer implements Runnable {   private final BlockingQueue queue;   Producer(BlockingQueue q) { queue = q; }   public void run() {     try {       while (true) { queue.put(produce()); }     } catch (InterruptedException ex) { ... handle ...}   }   Object produce() { ... } } class Consumer implements Runnable {   private final BlockingQueue queue;   Consumer(BlockingQueue q) { queue = q; }   public void run() {     try {       while (true) { consume(queue.take()); }     } catch (InterruptedException ex) { ... handle ...}   }   void consume(Object x) { ... } } class Setup {   void main() {     BlockingQueue q = new SomeQueueImplementation();     Producer p = new Producer(q);     Consumer c1 = new Consumer(q);     Consumer c2 = new Consumer(q);     new Thread(p).start();     new Thread(c1).start();     new Thread(c2).start();   } }}
所以在Volley中,request就是產品,主線程是生產者,NetworkDispatcher線程是消費者。

另外注意到,NetworkDispatcher的實現其實是策略設計模式:
/** The queue of requests to service. */    private final BlockingQueue<Request> mQueue;    /** The network interface for processing requests. */    private final Network mNetwork;    /** The cache to write to. */    private final Cache mCache;    /** For posting responses and errors. */    private final ResponseDelivery mDelivery;    /** Used for telling us to die. */    private volatile boolean mQuit = false;    /**     * Creates a new network dispatcher thread.  You must call {@link #start()}     * in order to begin processing.     *     * @param queue Queue of incoming requests for triage     * @param network Network interface to use for performing requests     * @param cache Cache interface to use for writing responses to cache     * @param delivery Delivery interface to use for posting responses     */    public NetworkDispatcher(BlockingQueue<Request> queue,            Network network, Cache cache,            ResponseDelivery delivery) {        mQueue = queue;        mNetwork = network;        mCache = cache;        mDelivery = delivery;    }
NetworkDispatcher建構函式的幾個參數都是介面,而run方法則使用這些策略類方法實現了演算法的主體流程,具體實現有些留給了開發人員,有些則是架構實現。比如ImageCache作為一級緩衝的Cache方法留給了開發人員實現,由開發人員控制具體的緩衝策略,當然Volley建議我們使用LRUCache作為L1緩衝的實現。
最後,NetworkDispatcher的數組則構成了RequestQueue類中線程池,由RequestQueue統一啟動和停止:
/**     * Creates the worker pool. Processing will not begin until {@link #start()} is called.     *     * @param cache A Cache to use for persisting responses to disk     * @param network A Network interface for performing HTTP requests     * @param threadPoolSize Number of network dispatcher threads to create     * @param delivery A ResponseDelivery interface for posting responses and errors     */    public RequestQueue(Cache cache, Network network, int threadPoolSize,            ResponseDelivery delivery) {        mCache = cache;        mNetwork = network;        mDispatchers = new NetworkDispatcher[threadPoolSize];        mDelivery = delivery;    }/**     * Starts the dispatchers in this queue.     */    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();        }    }



關於volley的網路請求部分可以看部落格:http://www.cnblogs.com/bvin/p/3291611.html



網路請求中有幾個轉換解析請求擷取響應結果的地方:

1.HttpStack介面的performRequest()方法

public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)

2.Network介面的performRequest()方法

public NetworkResponse performRequest(Request<?> request)

3.Request類的parseNetworkResponse()抽象方法

abstract protected Response<T> parseNetworkResponse(NetworkResponse response);

可以很鮮明得看出第一個是對原生Http請求的解析,解析出來是一個Apach的HttpResponse 執行個體,這個結果就是通過上述兩個HttpStack的實作類別HttpClientStack和HurlStack執行擷取的擷取的。

而第二個解析出來是架構自定的NetworkResponse,這是通過Network的實作類別BasicNetwork來擷取的。

第三個就是第二個得出來NetworkResponse解析成使用者期望Response<T> 了,這個Response和Request是對應的,有String型,json型還有Image型的。然後在通過ResponseDelivery把解析好的結果發送到主線程。

從Request到Response就是這樣一步步走來的。


再詳細看下上面第二個方法performRequest(),這個方法是NetworkDispatcher中主要步驟的第二步,其中真正網路請求的工作委託給mHttpStack實現,通過HttpStack實作類別就是的兩個HttpClientStack和HurlStack執行。這兩個類的區別在於HttpClientStack是直接用HttpClient的execute()方法執行一個Http請求,而HurlStack就是直接用URL.openConnection()進行串連,這種方式在2.3以上是不能用的,所以分開了這兩種方式。
/**     * @title performRequest執行各種Request請求並以NetworkResponse的形式返回結果     * @param Request     * @return NetworkResponse     * @throws VolleyError     * 定義:{@link Network#performRequest(Request)}     * 被調:{@link NetworkDispatcher#run()}     *      */    @Override//NetworkDispatcher的run()方法中調用    public NetworkResponse performRequest(Request<?> request) throws VolleyError {        long requestStart = SystemClock.elapsedRealtime();//開始請求時間        while (true) {            HttpResponse httpResponse = null;//apache的請求結果            byte[] responseContents = null;//請求的內容            Map<String, String> responseHeaders = new HashMap<String, String>();//響應結果頭部資訊            try {                // Gather headers.                Map<String, String> headers = new HashMap<String, String>();//儲存快取資料                addCacheHeaders(headers, request.getCacheEntry());//先擷取快取資料                httpResponse = mHttpStack.performRequest(request, headers);//去調用mHttpStack的實現方法執行請求                StatusLine statusLine = httpResponse.getStatusLine();//擷取http狀態線                int statusCode = statusLine.getStatusCode();//擷取狀態代碼                responseHeaders = convertHeaders(httpResponse.getAllHeaders());                // Handle cache validation.//處理緩衝驗證                if (statusCode == HttpStatus.SC_NOT_MODIFIED) {//返回快取資料                    return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED,                            request.getCacheEntry().data, responseHeaders, true);                }                //把HttpEntity轉化為byte[]資料                responseContents = entityToBytes(httpResponse.getEntity());                // if the request is slow, log it.//如果請求很慢,就列印出來看一下                long requestLifetime = SystemClock.elapsedRealtime() - requestStart;                logSlowRequests(requestLifetime, request, responseContents, statusLine);//列印                //串連正常但是返回無內容,拋出IO異常                if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_NO_CONTENT) {                    throw new IOException();                }                return new NetworkResponse(statusCode, responseContents, responseHeaders, false);            } catch (SocketTimeoutException e) {//讀取逾時,重試                attemptRetryOnException("socket", request, new TimeoutError());            } catch (ConnectTimeoutException e) {//連線逾時,重試                attemptRetryOnException("connection", request, new TimeoutError());            } catch (MalformedURLException e) {//Bad URL                throw new RuntimeException("Bad URL " + request.getUrl(), e);            } catch (IOException e) {//IO異常                int statusCode = 0;                NetworkResponse networkResponse = null;                if (httpResponse != null) {                    statusCode = httpResponse.getStatusLine().getStatusCode();                } else {//如果沒有返回httpResponse,就說明沒串連                    throw new NoConnectionError(e);                }                VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());                if (responseContents != null) {//返回資料不為空白                    networkResponse = new NetworkResponse(statusCode, responseContents,                            responseHeaders, false);//建立響應體                    if (statusCode == HttpStatus.SC_UNAUTHORIZED ||                            statusCode == HttpStatus.SC_FORBIDDEN) {//認證失敗異常,重試                        attemptRetryOnException("auth",                                request, new AuthFailureError(networkResponse));                    } else {//伺服器異常                        // TODO: Only throw ServerError for 5xx status codes.                        throw new ServerError(networkResponse);//只有狀態代碼為5XX才拋出伺服器異常                    }                } else {//網路異常                    throw new NetworkError(networkResponse);                }            }        }    }

A:先是通過mHttpStack把請求執行並且擷取它的響應結果,根據HttpStatus做出各種判斷。

B:然後再把httpResponse的Entity轉化為ByteArray,並處理各種發生的異常。

C:最後的過程是這樣的:通過Volley建立一個RequestQueue請求隊列,當這個隊列開始運作的時候會啟動NetworkDispatcher這個背景工作執行緒,而BasicNetwork的performRequest()的方法就在NetworkDispatcher線程run()方法中調用,然後通過mHttpStack的performRequest()方法擷取一個networkResponse,在NetworkDispatcher線程把這個networkResponse轉化為期望的資料類型,比如Response<String>,Response<Json>,Response<Bitmap>。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.