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

相关推荐
vistaup17 分钟前
OKHTTP 默认构建包含 android 4.4 的TLS 1.2 以及设备时间不对兼容
android·okhttp
bug-0077 天前
关于前后端自动携带cookie跨域问题
okhttp
weixin_440784118 天前
Java线程池工作原理浅析
android·java·开发语言·okhttp·android studio·android runtime
weixin_440784118 天前
OkHttp使用指南
android·java·okhttp
闻哥9 天前
从 AJAX 到浏览器渲染:前端底层原理与性能指标全解析
java·前端·spring boot·ajax·okhttp·面试
猿小羽11 天前
OkHttp vs Retrofit 技术分析报告 - 1769404939594
http·okhttp·retrofit·csdn
AylKfTscDFw11 天前
EtherCAT总线轴控制,大型非标组装检测设备成熟设备程序,注释非常详细,组合应用日本进口机...
okhttp
龙信科技14 天前
【国内电子数据取证厂商龙信科技】Charles的简单介绍及基本配置
科技·okhttp
Filotimo_15 天前
那在HTML中,action是什么
前端·okhttp·html
灵感菇_16 天前
Android OkHttp框架全解析
android·java·okhttp·网络编程