We know that the Android system does not allow time-consuming operations in the UI thread to improve the real-time responsiveness of the program, otherwise a ANR exception occurs, so time-consuming tasks must be placed in a non-UI thread to execute. Android/java provides many classes to help you complete asynchronous operations such as thethread class, the Timer class, the Asynctask class, the Handlerthread class, and the executor interface . What are the occasions when these classes are used?
This article simply summarizes the multithreading types and solutions that are common in Android development, and compares and analyzes the different scenarios to better understand and apply these API interfaces.
1. Single Asynchronous task
[scene]: download an apk file and copy a large file.
[scheme]: Thread class, Asynctask class
[Compare]:
Asynctask provides onprogressupdate and OnPostExecute notification of the progress and results of the task execution of the caller, and can perform UI operations directly within the function.
While thread does not directly manipulate the UI interface, the UI can be updated via Activity.runonuithread or handler.
So thread is more suited to perform individual tasks that do not require frequent interaction with the UI, whereas Asynctask is the opposite.
2. Scheduled/recurring Tasks
[scene]: periodically refresh the UI (e.g. stopwatch) to keep the TCP heartbeat connection.
[scheme]:"Thread + Sleep", timers timer
[Compare]:
Sample "Thread + Sleep" scenario:
New Thread (New Runnable () {@Override public void run () {while (! EXIT) {thread.sleep (1000); Mlistener.ontimearrived ();} }}). Start ();
Neither can perform UI update operations in the loop, but must use Activity.runonuithread or handler to complete the UI update.
Because Ontimearrived () in the thread scheme is taking up part of the time, the timing of this scheme is inaccurate, and the timer , which is created by the system to create asynchronous notifications, is more accurate, so it is recommended to use a timer to complete the scheduled task.
3. Worker threads
After the worker thread is started, it is in a hibernation state waiting for "command"/"message", and when the command/message is received, it is placed in the command/message queue, and then the wake-up thread executes sequentially or in parallel.
[scene]:"producer-Consumer" mode, TCP server-side command handlers
[scheme]: "Thread + Condition/lock", Handlerthread class, thread pool executor
[Compare]:
The Handlerthread class is an Android system that provides a thread class that encapsulates the loop loop, which makes it easier to perform command/message interactions with Callerthread and workthread. of course, we can also use thread and condition/lock way to achieve the same effect, but to implement more code ourselves.
The previous two methods are "serial" in the Way of executing "command", if you want to provide concurrency, while opening and managing multiple threads to perform tasks, you might consider using executor.
The above is my simple summary of the Android multi-threaded programming, there is any unclear or incorrect place, welcome message or letter [email protected] exchange discussion.
This article is from the "Shadow Three People" blog, please be sure to keep this source http://ticktick.blog.51cto.com/823160/1564055
Android Development Practice: Multithreaded Programming summary