Several common asynchronous frameworks and network access frameworks are differentiated and compared.

Source: Internet
Author: User

Several common asynchronous frameworks and network access frameworks are differentiated and compared.
Part1:

In our program, some time-consuming tasks are not allowed to appear in the main thread, mainly to prevent the Anr (Application not Responding) caused by blocking the main thread ), some time-consuming tasks mainly include:

Network Access, slow disk operations, and time-consuming Algorithms

When the processing of an event by our main thread exceeds a certain period of time, the main thread will crash and report ANR,

Common Solution: Use the sub-Thread Technology to remove time-consuming tasks from the main thread

1. handler Mechanism

You only need to send the UI update parameters to the handleMessage in the defined Handler using sendMessage in the Child thread to update the UI in the main thread (Handler implements the jump from the Child thread to the main thread)

2. runOnUiThread Method

This method allows the current main thread to obtain cpu resources for UI updates (as to how to return data from the Child thread, there are many methods, such as calling the interface back and forth to obtain parameters)

3. Use the familiar AsyncTask class

AsyncTask usage and source code analysis


Part2: Network Access

You are familiar with the right HttpClient and HttpUrlConnection methods, including Volley, OkHttp, and AsyncHttpClient.

For the first two basic methods, asynchronous processing is not available, that is, we need to use them with the asynchronous Processing Framework in part1, otherwise, you will be able to accept the naked embarrassment of ANR.

Volley is capable of asynchronous access and the callback Method for access termination is in the main thread, in this way, you can directly use it independently from the asynchronous access framework (and replace the Universal-Image-Loader asynchronous loading of images) Volley Usage Details

AsyncHttpClient is an asynchronous encapsulation of HttpClient. One drawback of Volley is that the callback method is still in the Child thread. We still need to use the asynchronous framework in part1 to solve the problem.

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.