"Android Development Art Quest" Reading notes (11)--android Threads and thread pools

Source: Internet
Author: User

"Android Development Art Exploration" reading notes (10)--android message mechanism

NO1:

There are a lot of roles in Android that can play threading, such as Asynctask, Intentservice, Handlerthread.

The asynctask layer encapsulates the thread pool and handler, primarily for the convenience of developers updating the UI in child threads.

handlerthread is a thread that has a message loop and can use handler inside it.

Intentservice The bottom of the thread, is a service, the system has been encapsulated so that it can more easily perform background tasks, Intentservice internal use of handlerthread to perform tasks, Intentservice automatically exits when the task finishes executing.

No2:

The system dispatches each thread in a time-slice rotation .

No3:

The role of the main thread is to run four components and handle them and the user's interaction, the role of the sub-thread is to perform time-consuming tasks.

No4:

Asynctask is a lightweight asynchronous task class that is not suitable for particularly time-consuming background tasks, and it is recommended to use a thread pool for particularly time-consuming tasks

NO5:

Asynctask Core Approach :

1)OnPreExecute(): main thread execution, ready to work

2)Doinbackground(params...params): Execute in thread pool, this method can call Publishprogress method to update task progress

3)onprogressupdate(progress...values): main thread execution, called when background task execution progress has changed

4)OnPostExecute(result result): Main thread execution, return value

5)oncancelled(): Called when an asynchronous task is canceled

NO6:

Asynctask restriction conditions :

1) The Asynctask class must be loaded in the main thread

2) The Asynctask object must be created in the main thread

3) The Execute method must be called in the UI thread

4) do not call OnPreExecute (), OnPostExecute, Doinbackground, and Onprogressupdate methods directly in the program

5) A Asynctask object can only be executed once, that is, the Execute method can be called only once, otherwise it will report a run-time exception

6) before Android1.6, Asynctask serial execution , 1.6 with thread pool for parallel tasks, 3.0 to avoid concurrency errors, and serial. But we can perform the task in parallel through Asynctask's executeonexecutor.

No7:

Asynctask Working principle

Asynctask.execute-->

Asynctask.executeonexecutor-->

Asynctask.onpreexecute-->

Asynctask.doinbackground-->

Asynctask.postresult-->

No8:

There are two thread pools in Asynctask serialexecutor and thread_pool_executor and one handler (Internalhandler)

The thread pool Serialexecutor is used for task queuing, and the thread pool thread_pool_executor is used to actually perform tasks, Internalhandler is used to switch the execution environment from the thread pool to the main thread.

No9:

Android3.0 and above, you can use the Asynctask executeonexecutor method, which is the new method Android3.0 added, cannot be used on the lower version.

No10:

Handlerthread inherits the thread, which is a thread that can be used with handler, by implementing the method:

 Public void run () {    = process.mytid ();    Looper.prepare ();     synchronized (this) {        = looper.mylooper ();        Notifyall ();    }    Process.setthreadpriority (mpriority);    Onlooperprepared ();    Looper.loop ();     =-1;}

NO11:

The normal thread is primarily used to perform a time-consuming task in the Run method, and Handlerthread creates a message queue internally, and the outside world needs to notify Handlerthread to perform a specific task by handler message mode.

No12:

Intentservice is a special service that inherits the service and is an abstract class, so you must create its subclasses to use Intentservice.

NO13:

Benefits of the thread pool

1) Reuse threads in the thread pool to avoid the performance overhead associated with creating and destroying threads

2) can effectively control the maximum concurrency of the thread pool, avoid the congestion caused by the large number of threads that preempt the system resources.

3) Ability to perform simple management of threads and provide functions such as timed execution and specified interval loop execution

NO14:

Executor is an interface where the real thread pool is implemented as Threadpoolexecutor.

NO15:

Thread Pool Threadpoolexecutor

"Android Development Art Quest" Reading notes (11)--android Threads and thread pools

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.