okhttp系列-execute过程

1.RealCall.execute

final class RealCall implements Call {
    @Override 
    public Response execute() throws IOException {
        synchronized (this) {
      i    if (executed) throw new IllegalStateException("Already Executed");
          executed = true;
        }
        transmitter.timeoutEnter();
        transmitter.callStart();
        try {
            client.dispatcher().executed(this); //1.调用Dispatcher的executed
            return getResponseWithInterceptorChain();//2调用getResponseWithInterceptorChain
        } finally {
            client.dispatcher().finished(this); //3.结束
        }
    }
}
1.1.Dispatcher.executed

将call加入runningSyncCalls

public final class Dispatcher {
    private final Deque<RealCall> runningSyncCalls = new ArrayDeque<>();

    synchronized void executed(RealCall call) {
        runningSyncCalls.add(call);
    }
}
1.2.RealCall.getResponseWithInterceptorChain

执行请求后返回结果

final class RealCall implements Call {
    Response getResponseWithInterceptorChain() throws IOException {
        Interceptor.Chain chain = new RealInterceptorChain(interceptors, transmitter, 
        null, 0, originalRequest, this, client.connectTimeoutMillis(),
        client.readTimeoutMillis(), client.writeTimeoutMillis());

        Response response = chain.proceed(originalRequest); //执行拦截器
        return response; //返回结果
    }
}
1.3.Dispatcher.finished

从runningSyncCalls移除call;

执行下一个call;

public final class Dispatcher {
    void finished(RealCall call) {
        finished(runningSyncCalls, call);
    }

    private <T> void finished(Deque<T> calls, T call) {
        Runnable idleCallback;
        synchronized (this) {
            //将call从calls移除
            if (!calls.remove(call)) throw new AssertionError("Call wasn't in-flight!");
            idleCallback = this.idleCallback;
        }

        boolean isRunning = promoteAndExecute(); //执行下一个call

        if (!isRunning && idleCallback != null) {//如果没有要执行的call,调用idleCallback
            idleCallback.run(); 
        }
    }
}
1.4.总结
  • 将RealCall添加到runningSyncCalls
  • 调用getResponseWithInterceptorChain执行
  • finally里执行

       从将RealCall从runningSyncCalls移除

       调用promoteAndExecute,执行其他的AsyncCall

相关推荐

  1. okhttp系列-execute过程

    2023-12-06 18:00:03       51 阅读
  2. okhttp系列-enqueue过程

    2023-12-06 18:00:03       60 阅读
  3. okhttp系列-一些上限值

    2023-12-06 18:00:03       57 阅读
  4. <span style='color:red;'>OkHttp</span>

    OkHttp

    2023-12-06 18:00:03      43 阅读
  5. okhttp

    2023-12-06 18:00:03       27 阅读
  6. OkHttp介绍

    2023-12-06 18:00:03       55 阅读
  7. Android OkHttp

    2023-12-06 18:00:03       34 阅读
  8. <span style='color:red;'>OkHttp</span>3

    OkHttp3

    2023-12-06 18:00:03      20 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-06 18:00:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-06 18:00:03       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-06 18:00:03       87 阅读
  4. Python语言-面向对象

    2023-12-06 18:00:03       96 阅读

热门阅读

  1. 用户在页面离开时发送http请求,如何成功

    2023-12-06 18:00:03       50 阅读
  2. Docker 基本管理

    2023-12-06 18:00:03       38 阅读
  3. 打印HTML页面,表格元素被截断的解决方案

    2023-12-06 18:00:03       55 阅读
  4. Vue3 toRef,toRefs | toRaw

    2023-12-06 18:00:03       53 阅读
  5. LeetCode双指针:有序数组中的单一元素

    2023-12-06 18:00:03       64 阅读