What is AsyncTask
It is essentially an asynchronous framework that encapsulates thread pools and handlers
Ii. The use of AsyncTask
- Three parameters
- Parameter 1 Integer Indicates the parameter that can be used in background tasks when AsyncTask is executed
- Parameter 2 Integer Displays the current progress on the interface when a task is executed in the background
- Parameter 3 String If you need to return the result after an asynchronous task is executed, String is used as the returned value
- Five methods
- onPreExecute
- doInBackground
- publishProgress
- onProgressUpdate
- onPostExecute
Public class UpdateInfoAsyncTask extends AsyncTask<Integer, Integer, String> {/** * parameter 1 Integer Specifies the parameter passed when an AsyncTask is executed. * Parameter 2 Integer Displays the current progress in background tasks. * Parameter 3 String If you need to return the result after an asynchronous task is executed, use String as the returned value. */ / Before time-consuming operations are performed, initialize the task. @override protected void onPreExecute() {super.onpreexecute (); } @override protected String doInBackground(Integer... integers) { int i; for (i = 10; i <= 100; I +=10) {// publishProgress(I); } return i + integers[0].intValue() + ""; } // call @override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); @override protected void onPostExecute(String s) {super.onPostExecute(s); }}Copy the code
Iii. Internal principle of AsyncTask
- The essence of AsyncTask is a static thread pool. Subclasses of AsyncTask can implement different asynchronous tasks. These tasks are submitted to the static thread pool for execution
- The worker thread of the thread pool executes the doInBackgroud(mParams) method to perform the asynchronous task
- When the task status changes, the worker thread sends a message to the UI thread. InternalHandler within AsyncTask responds to the message and calls the relevant callback function.
4. Precautions of AsyncTask
- A memory leak
Similar to Handler’s memory leak, a non-static inner class holds a reference to an external class. Since the activity is destroyed while the AsyncTask is still executing the task, the AsyncTask will hold a reference to the activity, making it unable to be recycled when the activity wants to be destroyed. Caused a memory leak. Fix memory leaks: Weak references, static inner classes, call asyncTask cancle method 2 in onDestroy in the activity. A lifecycle AsyncTask is not destroyed with the activity’s destruction. It is not destroyed unless the AsyncTask cancle method is called. 3. The result is lost when the screen rotates or the activity is killed due to insufficient memory, causing the activity to be recreated and the previously running AsyncTask to hold the previous activity reference, but the previous reference is invalid. Therefore, calling the AsyncTask onPostExecute method to update the interface does not take effect. It could cause a crash. Before Android 1.6, AsyncTask is serial, which puts all tasks in a thread pool in an orderly manner. However, after version 1.6 to 2.3, AsyncTask is changed to parallel, and after version 2.3, it is changed to serial. However, you can also call the Excute method for parallelism. It is recommended to use serial only in general. This will keep the entire thread pool stable. Thread pools are unstable if they are concurrent, albeit efficiently. AsyncTask cannot perform highly concurrent and time-consuming operations.