OKHTTP3 Task Queue

Source: Internet
Author: User

OKHTTP3 has two modes of operation:

1. Synchronous blocking call and return directly;

2. Implement non-blocking asynchronous callbacks through the internal thread pool distribution schedule;

The following is a non-blocking asynchronous callback, okhttp in a multi-concurrent network distribution scheduling process, mainly Dispatcher objects:

Multithreading: Multithreading technology mainly solves the problem of multiple threads in the processor unit, which can significantly reduce the idle time of the processor unit and increase the throughput capacity of the processor unit. However, if you use multithreading incorrectly, you increase the processing time for individual tasks

threadpool thread pool : The key to the threads pool is the threading reuse to reduce the loss of non-core tasks.

Like what:

t = t1+t2+t3 where T1 and T3 are the overhead of multithreading itself (in Java, by mapping Pthead and further systemcall threads through native), we are eager to reduce the time spent in T1,T3, thus reducing t time. However, users of some threads do not notice this, so the threads are created or destroyed frequently in the program, which causes T1 and T3 to occupy a considerable proportion in T. Obviously this is the highlight of the thread's weakness (T1,T3), not the merit (concurrency).

thread pooling technology is a technique that focuses on how to shorten or adjust t1,t3 time to improve the performance of server programs.

1. Reduce the time lost in creating destruction by caching threads;

2. By controlling the number of thread thresholds, the pressure on the system calls to the JVM's memory and thread switching is reduced when the thread is too young for idle CPU (for example, long-time cards are on I/O) and too many threads.

Construct a singleton thread pool:

Public synchronized Executorservice executorservice (){
if (Executorservice = = null) {
Executorservice = new Threadpoolexecutor (0, Integer.max_value, Timeunit.seconds,
New Synchronousqueue<runnable> (), Util.threadfactory ("OkHttp Dispatcher", false));
}
return executorservice;
}
Parameter description:
    • int corepoolsize: The minimum number of concurrent threads, which concurrently includes both idle and active threads, and if 0, all thread threads will be destroyed after idle time.
    • int maximumpoolsize: The maximum number of threads, the maximum thread value that can be expanded when a task comes in, and when it is greater than this value will be handled according to the discard processing mechanism
    • Long KeepAliveTime: When the number of threads is greater than the corePoolSize maximum surviving time of the extra idle thread, similar to the keep-alive in HTTP
    • Timeunit Unit: Time unit, usually in seconds
    • Blockingqueue<runnable> workQueue: Work queue, FIFO, you can see that the priority queue is not set up like Picasso. (Blocking queue)
    • Threadfactory threadfactory: A single thread of the factory, can log, set Daemon (that is, when the JVM exits, the thread ends automatically) and so on.
As you can see, in Okhttp, a thread pool with a threshold of [0, Integer.max_value] is built, it does not retain any minimum number of threads, creates more threads at any time, and when the thread is idle for only 60 seconds, it uses a blocking work queue that does not store elements, a block called " OkHttp Dispatcher "Thread factory. That is, in the actual operation, when 10 concurrent requests are received, the line pool creates 10 threads, and when the work is done, the thread pool closes all threads successively after 60s.Reverse proxy model:

In Okhttp, a reverse proxy and distribution technology similar to Nginx is used, which is a typical single-producer multi-consumer problem. We know that in Nginx, the user through the HTTP (Socket) to access the front server, the server will add the header and automatically forward the request to the backend cluster, and then return the data results to the user (such as the last time the book was hung and also showed the nginx error). By assigning work to multiple back-end servers and sharing sessions, you can increase the load-balancing capability of the service, achieve non-blocking, high availability, high concurrent connections , and avoid the load, speed, and online rate of all resources placed on a single server.

And in Okhttp, very similar to the above scenario, it uses dispatcher as the dispatcher of the task, the thread pool corresponds to multiple backend servers, withAsyncCallcorresponding to the socket request, use theDeque<readyAsyncCalls>Internal cache corresponding to Nginx

Specific members are as follows

    • maxrequests = 64: Maximum number of concurrent requests is 64
    • Maxrequestsperhost = 5: The maximum number of requests per host is 5
    • Dispatcher: Distributor, i.e. producer (default in main thread)
    • Asynccall: runnable to be processed in the queue (the asynchronous callback interface is wrapped)
    • Executorservice: Consumer pool (i.e. thread pool)
    • DEQUE<READYASYNCCALLS>: Cache (implemented with arrays, auto-expandable, no size limit)
    • DEQUE<RUNNINGASYNCCALLS>: A running task is simply a reference to a running task to determine the concurrency, note that it is not a consumer cache

You can significantly reduce I/O latency by distributing the request task to multiple threads

Okhttp task scheduling When we want to use Okhttp's asynchronous request, we typically construct the following: When HttpClient's request is queued, we can see that the dispatcher is actually queued, according to the code.
synchronized void enqueue(AsyncCall call) {

//添加正在运行的请求
 runningAsyncCalls.add(call);
 //线程池执行请求
  executorService().execute(call);

//添加到缓存队列排队等待
readyAsyncCalls.add(call);

}
You can see if the request entered the cache as follows:(runningRequests<64 && runningRequestsPerHost<5)If the condition is met, then direct theAsyncCallAdd directly torunningCallsQueue and executes in a thread pool (the thread pools are automatically created, destroyed, and the corresponding threads are cached according to the current load). Instead, putreadyAsyncCallsThe cache waits.

We re-parse the request element Asynccall (which implements the Runnable interface), which implements the Execute method internally as follows:

When a task is completed, whether or not an exception occurs,finallyThe code snippet is always executed, that is, the finished function that calls dispatcher, opens the source code, discovers that it removes the running task call from the queue runningasynccalls, and then executespromoteCalls()In this way, the cache queue is proactively moved forward without the use of complex encodings such as mutex locks.

Through the above analysis, we know:

    1. Okhttp uses dispatcher technology, similar to Nginx, with the thread pool to achieve high concurrency, low-blocking operation
    2. Okhttp uses deque as the cache, in order to first out in the queue
    3. The best thing about okhttp is that it calls functions in the try/finally to finished proactively control the movement of the waiting queue, rather than locking or wait/notify, greatly reducing the complexity of the coding.
Address:
http://www.jianshu.com/p/aad5aacd79bf

 






 



OKHTTP3 Task Queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.