Example #1
0
 /** Stops the cache and network dispatchers. */
 public void stop() {
   if (mCacheDispatcher != null) {
     mCacheDispatcher.quit();
   }
   for (int i = 0; i < mDispatchers.length; i++) {
     if (mDispatchers[i] != null) {
       mDispatchers[i].quit();
     }
   }
 }
Example #2
0
  /**
   * Starts the dispatchers in this queue.
   *
   * <p>start 方法中,开启一个缓存调度线程CacheDispatcher和 n 个网络调度线程NetworkDispatcher,这里 n 默认为4, 存在优化的余地,比如可以根据
   * CPU 核数以及网络类型计算更合适的并发数。 缓存调度线程不断的从缓存请求队列中取出 Request 去处理,网络调度线程不断的从网络请求队列中取出 Request 去处理。
   */
  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();
    }
  }