Http RxJava + Retrofit + OkHttp

OKHttp全解析系列(四) -- 线程池和消息队列

2017-11-01  本文已影响653人  嘎啦果安卓兽

线程池

Dispatcher#executorService()

  public synchronized ExecutorService executorService() {
    if (executorService == null) {
      executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS,
          new SynchronousQueue<Runnable>(), Util.threadFactory("OkHttp Dispatcher", false));
    }
    return executorService;
  }

OkHttp实现的是无边界限制的线程池。
参数解析:

同步请求流程

  @Override public Response execute() throws IOException {
    synchronized (this) {
      if (executed) throw new IllegalStateException("Already Executed");
      executed = true;
    }
    captureCallStackTrace();
    eventListener.callStart(this);
    try {
      /*
      Dispatcher.executed 方法 执行入队操作
      synchronized void executed(RealCall call) {
        runningSyncCalls.add(call);
      }
       */
      client.dispatcher().executed(this);
      //进入拦截器链流程,然后进行请求,获取Response
      Response result = getResponseWithInterceptorChain();
      if (result == null) throw new IOException("Canceled");
      eventListener.callEnd(this, null);
      return result;
    } catch (IOException e) {
      eventListener.callEnd(this, e);
      throw e;
    } finally {
      /*
      Dispatcher.finished方法  做的是出队操作
      void finished(RealCall call) {
        finished(runningSyncCalls, call, false);
      }
       */
      client.dispatcher().finished(this);
    }
  }

同步请求总结

直接进入拦截器链流程进行请求,获取Response

异步调用流程

异步调用用的是 AsyncCall,AsyncCall是RealCall类的内部类

  1. 调用 RealCall#enqueue 方法
//关键代码 非源码
  @Override public void enqueue(Callback responseCallback) {
    client.dispatcher().enqueue(new AsyncCall(responseCallback));
  }
  1. 调用 Dispatcher#enqueue 方法
  synchronized void enqueue(AsyncCall call) {
    //入队条件:当前正在运行的AsyncCall < maxRequests(64) && 同一个Host对应的AsyncCall < maxRequestsPerHost(5)
    if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
      //入队
      runningAsyncCalls.add(call);
      //将该call 加入线程池执行  进行网络请求的过程基本与同步请求类似
      executorService().execute(call);
    } else {
      //不满足条件 则加入等待队列
      readyAsyncCalls.add(call);
    }
  }

readyAsyncCalls 被恢复到runningAsyncCalls 的过程

Dispatcher#promoteCalls方法

  private void promoteCalls() {
    //当前正在执行的线程超限 return
    if (runningAsyncCalls.size() >= maxRequests) return; // Already running max capacity.
    //readyAsyncCalls 为空 return
    if (readyAsyncCalls.isEmpty()) return; // No ready calls to promote.

    for (Iterator<AsyncCall> i = readyAsyncCalls.iterator(); i.hasNext(); ) {
      AsyncCall call = i.next();

      //同一个Host对应的AsyncCall < maxRequestsPerHost(5) 
      if (runningCallsForHost(call) < maxRequestsPerHost) {
        //从readyAsyncCalls中移除
        i.remove();
        //将该 call 加入到runningAsyncCalls中
        runningAsyncCalls.add(call);
        //将该call 加入线程池执行 
        executorService().execute(call);
      }

      //如果runningAsyncCalls满了就return  否则继续循环
      if (runningAsyncCalls.size() >= maxRequests) return; // Reached max capacity.
    }
  }
Dispatcher#promoteCalls方法 触发的时机

1.Dispatcher#setMaxRequestsPerHost 被调用时 即每个host对应的最大请求数改变时
2.Dispatcher#setMaxRequests 被调用时 即 同时进行的最大请求数改变时
3.Dispatcher#finished 被调用时 即一条请求结束,执行了finish()的出队操作时

异步请求总结

如果满足入队条件,则加入到线程池中进行请求。否则会加入到等待队列中进行等待,等待队列中的请求在某些条件被触发时会被移入到正在请求队列,并加入到线程池进行执行。

参考资料

http://www.jianshu.com/p/074dff0f4ecb

上一篇 下一篇

猜你喜欢

热点阅读