1.RealCall.enqueue

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调用Dispatcherenqueue
    client.dispatcher().enqueue(new AsyncCall(responseCallback));
  }
}

2.Dispatcher.enqueue

public final class Dispatcher {
    private final Deque&lt;AsyncCall&gt; readyAsyncCalls = new ArrayDeque<&gt;();

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

3.Dispatcher.promoteAndExecute

public final class Dispatcher {
    private boolean promoteAndExecute() {
        assert (!Thread.holdsLock(this));

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

                // Host max capacity.
                //超过了一个主机最大请求数5
                if (asyncCall.callsPerHost().get() &gt;= 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也是在线触发

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.总结

       遍历readyAsyncCalls

       从readyAsyncCalls移除AsyncCall

       将AsyncCall添加到executableCalls

       将AsyncCall添加到runningAsyncCalls

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

原文地址:https://blog.csdn.net/lostfish123/article/details/134686298

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_40492.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注