Beispiel #1
0
  /**
   * Called from {@link Request#finish(String)}, indicating that processing of the given request has
   * finished.
   *
   * <p>Releases waiting requests for <code>request.getCacheKey()</code> if <code>
   * request.shouldCache()</code>.
   *
   * <p>(1). 首先从正在进行中请求集合mCurrentRequests中移除该请求。 (2).
   * 然后查找请求等待集合mWaitingRequests中是否存在等待的请求,如果存在,则将等待队列移除,
   * 并将等待队列所有的请求添加到缓存请求队列中,让缓存请求处理线程CacheDispatcher自动处理。
   */
  <T> void finish(Request<T> request) {
    // Remove from the set of requests currently being processed.
    synchronized (mCurrentRequests) {
      mCurrentRequests.remove(request);
    }
    synchronized (mFinishedListeners) {
      for (RequestFinishedListener<T> listener : mFinishedListeners) {
        listener.onRequestFinished(request);
      }
    }

    if (request.shouldCache()) {
      synchronized (mWaitingRequests) {
        String cacheKey = request.getCacheKey();
        Queue<Request<?>> waitingRequests = mWaitingRequests.remove(cacheKey);
        if (waitingRequests != null) {
          if (VolleyLog.DEBUG) {
            VolleyLog.v(
                "Releasing %d waiting requests for cacheKey=%s.", waitingRequests.size(), cacheKey);
          }
          // Process all queued up requests. They won't be considered as in flight, but
          // that's not a problem as the cache has been primed by 'request'.
          mCacheQueue.addAll(waitingRequests);
        }
      }
    }
  }
Beispiel #2
0
 @SuppressWarnings("unchecked")
 public <T> void finish(Request<T> request) {
   synchronized (mFinishedListeners) {
     for (RequestFinishedListener listener : mFinishedListeners) {
       listener.onRequestFinished(request);
     }
   }
 }