Category:okhttp网络框架源码解析
Article From:https://www.cnblogs.com/webor2006/p/9063363.html

A detailed analysis of the synchronization request was carried out in the last http://www.cnblogs.com/webor2006/p/8023967.html, this time to analyze the asynchronous request, and the way the asynchronous request and the synchronous request are used is basically poor.Not much. Look back at the following:

The steps of its asynchronous request are as follows:

1、Create a OkHttpClient object. [as synchronous request]

2、Build a Request object and build Call objects through OkHttpClient and Request objects.【The same as the synchronization request]

3、Execute the enqueue method of Call.

  • Judge the current Call.
    Here is an analysis of the specific implementation of this enqueue:

    That is to say, the same request can only be carried out once and cannot be repeated.

  • Encapsulate it into a AsyncCall object.

    Before calling the enqueue () method, it will encapsulate a AsyncCall object. What is AsyncCall?

  • client.dispatcher().enqueue()。
    Then came the core method of asynchronous request, and began to explore:

    Then a line of analysis was made.

    Several variables are used, first of all, to understand:

    This is very obvious to ensure that the line pool object is a single example, and that the second parameter maximumPoolSize is currently Integer.MAX_VALUE, and if there are thousands of concurrent asynchronous requests coming, it is not to have to open thousands of threads?In fact, it is not. In the front, we also analyzed a maximum request limit, and the maximum number of concurrent asynchronous requests will not exceed 64.

    How do AsyncCall threads perform asynchronous requests?

    So the incoming parent looks at it:

     So back to AsyncCall’s execute () implementation:

    Then it analyzes it concretely.

     

     


    Why does it use it? It is actually used to adjust the queue for the entire asynchronous request, because the queue used is non thread safe, so we can see that the sentence is wrapped in sync code.

     At this point, the whole asynchronous request flow analysis is completed.The callback method runs in the subthread,As follows:

Leave a Reply

Your email address will not be published. Required fields are marked *