okhttp系列-enqueue过程

1.RealCall.enqueue

java 复制代码
final class RealCall implements Call {
    @Override 
    public void enqueue(Callback responseCallback) {
    synchronized (this) {
        if (executed) throw new IllegalStateException("Already Executed");
        executed = true;
    }
    transmitter.callStart();

    //创建AsyncCall,调用Dispatcher的enqueue
    client.dispatcher().enqueue(new AsyncCall(responseCallback));
  }
}

2.Dispatcher.enqueue

java 复制代码
public final class Dispatcher {
    private final Deque<AsyncCall> readyAsyncCalls = new ArrayDeque<>();

    void enqueue(AsyncCall call) {
        synchronized (this) {
            readyAsyncCalls.add(call); //将call添加到readyAsyncCalls
        }
        promoteAndExecute(); //调用执行的方法
    }
}

3.Dispatcher.promoteAndExecute

java 复制代码
public final class Dispatcher {
    private boolean promoteAndExecute() {
        assert (!Thread.holdsLock(this));

        List<AsyncCall> executableCalls = new ArrayList<>();
        boolean isRunning;
        synchronized (this) {
            //遍历readyAsyncCalls
            for (Iterator<AsyncCall> i = readyAsyncCalls.iterator(); i.hasNext(); ) {
                AsyncCall asyncCall = i.next();
                //如果正在执行的call超过了最大数目64,则不执行
                if (runningAsyncCalls.size() >= maxRequests) break; // Max capacity.

                // Host max capacity.
                //超过了一个主机的最大请求数5
                if (asyncCall.callsPerHost().get() >= maxRequestsPerHost) continue; 

                //从readyAsyncCalls remove
                i.remove();
                asyncCall.callsPerHost().incrementAndGet();
                //添加到executableCalls
                executableCalls.add(asyncCall);
                //添加到runningAsyncCalls
                runningAsyncCalls.add(asyncCall);
            }
            isRunning = runningCallsCount() > 0;
        }

        for (int i = 0, size = executableCalls.size(); i < size; i++) {
            AsyncCall asyncCall = executableCalls.get(i);
            //执行
            asyncCall.executeOn(executorService());
        }

        return isRunning;
    }
}

4.AsyncCall执行

AsyncCall其实是一个Runnable,所以最终执行的是AsyncCall的execute

整个execute()都是在线程里执行的,所以onResponse和onFailure也是在线程触发的

java 复制代码
final class RealCall implements Call {
    @Override 
    protected void execute() {
        try {
            Response response = getResponseWithInterceptorChain();
            //触发callback,返回response
            responseCallback.onResponse(RealCall.this, response);
        } catch (Throwable t) {
            responseCallback.onFailure(RealCall.this, canceledException);
        } finally {
             client.dispatcher().finished(this);//从runningAsyncCalls移除
        }
    }
}

5.总结

  • 创建AsyncCall
  • 将AsyncCall放入readyAsyncCalls
  • 执行

遍历readyAsyncCalls

从readyAsyncCalls移除AsyncCall

将AsyncCall添加到executableCalls

将AsyncCall添加到runningAsyncCalls

遍历executableCalls,执行里面的每一个AsyncCall

相关推荐
摇滚侠9 小时前
《SpringBoot 3:入门与应用实战》第 9 章 跨域问题 阅读笔记 27
spring boot·笔记·okhttp
刘广睿2 天前
断点续传与分片下载是怎么实现的?Range 请求在视频下载中的工程实践
okhttp·ffmpeg·音视频·下载·python3.11
泡海椒7 天前
JQuick-Curl vs RestTemplate / OkHttp / OpenFeign:框架选型对比,谁更适合第三方接口调用?
okhttp
Co_Hui11 天前
OkHttp 原理
okhttp
殷忆枫11 天前
BOA服务器下实现视频回放功能(CGI+软链接方案)
服务器·okhttp·音视频
消失的旧时光-194313 天前
第十篇:Ktor Custom Client Plugin:从 OkHttp Interceptor 真正理解请求与响应生命周期
okhttp·plugin·ktor·kmp
mmsx14 天前
基于 Android 的校园信息管理系统源码
android·java·okhttp
Full Stack Developme21 天前
跨站请求伪造 (CSRF) 是什么 设计及工作原理
前端·okhttp·csrf
weixin_4407841123 天前
【OkHttp实现原理】
android·java·okhttp
AZaLEan__1 个月前
原生XMLHttpRequest + axios + fetch
okhttp