0
点赞
收藏
分享

微信扫一扫

OkHttp3源码详解(六)Okhttp任务队列工作原理,安卓驱动工程师面试题

infgrad 2022-01-20 阅读 52
  1. final class AsyncCall extends NamedRunnable {}

所以Call本质就是一个Runable线程操作元肯定是放进excutorService中直接启动的。

2 线程池的复用和管理

2.1 图解

为了完成调度和复用,定义了两个队列分别用作等待队列和执行任务的队列。这两个队列都是Dispatcher 成员变量。Dispatcher是一个控制执行,控制所有Call的分发和任务的调度、通信、清理等操作。这里只介绍异步调度任务。

  1. /** Ready async calls in the order they’ll be run. */

  2. private final Deque readyAsyncCalls = new ArrayDeque<>();

  3. /** Running asynchronous calls. Includes canceled calls that haven’t finished yet. */

  4. private final Deque runningAsyncCalls = new ArrayDeque<>();

在《okhttp连接池复用机制》文章中我们在缓存Connection连接的时候也是使用的Deque双端队列。这里同样的方式,可以方便在队列头添加元素,移除尾部的元素。

image

2.2 过程分析

Call代用equeue方法的时候

  1. synchronized void enqueue(AsyncCall call) {

  2. if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {

  3. runningAsyncCalls.add(call);

  4. executorService().execute(call);

  5. } else {

  6. readyAsyncCalls.add(call);

  7. }

  8. }

方法中满足执行队列里面不足最大线程数maxRequests并且Call对应的host数目不超过maxRequestsPerHost 的时候直接把call对象直接推入到执行队列里,并启动线程任务(Call本质是一个Runnable)。否则,当前线程数过多,就把他推入到等待队列中
Call执行完肯定需要在runningAsyncCalls 队列中移除这个线程。那么readyAsyncCalls队列中的线程在什么时候才会被执行呢。

追溯下AsyncCall 线程的执行方法

  1. @Override

  2. protected void execute() {

  3. boolean signalledCallback = false;

  4. try {

  5. Response response = getResponseWithInterceptorChain(forWebSocket);

  6. if (canceled) {

  7. signalledCallback = true;

  8. responseCallback.onFailure(RealCall.this, new IOException(“Canceled”));

  9. } else {

  10. signalledCallback = true;

  11. responseCallback.onResponse(RealCall.this, response);

  12. }

  13. } catch (IOException e) {

  14. if (signalledCallback) {

  15. // Do not signal the callback twice!

  16. Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);

  17. } else {

  18. responseCallback.onFailure(RealCall.this, e);

  19. }

  20. } finally {

  21. client.dispatcher().finished(this);

  22. }

  23. }

  24. }

这里做了核心request的动作,并把失败和回复数据的结果通过responseCallback 回调到Dispatcher。执行操作完毕了之后不管有无异常都会进入到dispactcherfinished方法。

  1. private void finished(Deque calls, T call, boolean promoteCalls) {

  2. int runningCallsCount;

  3. Runnable idleCallback;

  4. synchronized (this) {

  5. if (!calls.remove(call)) throw new AssertionError(“Call wasn’t in-flight!”);

  6. if (promoteCalls) promoteCalls();

  7. runningCallsCount = runningCallsCount();

  8. idleCallback = this.idleCallback;

  9. }

  10. && idleCallback != null) {

  11. idleCallback.run();

  12. }

  13. }
    gCallsCount = runningCallsCount();

  14. idleCallback = this.idleCallback;

  15. }

  16. && idleCallback != null) {

  17. idleCallback.run();

  18. }

  19. }

举报

相关推荐

0 条评论