Chapter 11 Android thread and thread pool

In addition to the Thread, Android can play a role in the thread there are many, such as AsyncTask and IntentService, while HandlerThread also a special thread.

AsyncTask encapsulates the thread pool and Handler. HandlerThread is a thread having a message loop, may be used in its internal Handler. IntentService is a service, internal IntentService using HandlerThread to perform tasks, IntentService will automatically exit when the task is finished. IntentService is a service that is not likely to be killed so that the system can try to ensure that mission, if it is a background thread, no due process at this time the four components of the activity, it will lower the priority of this process, likely to be kill system, which is IntentService advantages.

11.1 the main thread and the child thread

From the beginning Android3.0 system requires network access must be performed in the child thread, or network access will fail and throw NetworkOnMainThreadException this exception.

Thread form in 11.2 Android

11.2.1 AsyncTask

AsyncTask is a lightweight asynchronous task class, it can perform background tasks in the thread pool, and then pass the progress and implementation of the final results to the main thread and update the UI in the main thread.

Four core methods:

  1. onPreExecute()
  2. doInBackground (Params ... params) In this method can update task progress by publishProgress method, publishProgress method calls onProgreeUpdate method.
  3. onProgressUpdate(Progress…values)
  4. onPostEcecute(Result result)
  5. onCancelled (), is invoked when the task was canceled, will not be called onPostExecute ()

AsyncTask in use, some restrictions:

  1. AsyncTask class must be loaded in the main thread
  2. AsyncTask objects must be created in the main thread
  3. execute method must be called on the UI thread
  4. Do not directly call onPreExecute (), onPostExecute, doInBackground, onProgressUpdate method
  5. A AsyncTask object can only be performed once, that can only be called once the execute method
  6. Before Android1.6, AyncTask serial mission, Android1.6 when AsyncTask began to use thread pool parallel tasks, but from the beginning Android3.0, AsyncTask and using a thread to perform tasks serially. In Android3.0 later versions, you can execute tasks in parallel by executeOnExecutor method of AsyncTask.

AsyncTask work lane flow chart:

11.2.3 HandlerThread

HandlerThread inherited Thread, Looper created in the run () method.

11.2.4 IntentService

IntentService is a special Service, inherited Service. It can be used to perform the time-consuming task (executed in a non-UI thread), when the task is executed it will stop automatically.

There are two ways to stop stopSelf services () and stopSelf (int startId). stopSelf () will immediately stop the service, stopSelf (int startId) will wait until all of the messages have been processed terminate the service (the principle is to determine whether the recently launched service id and startId equal).

Time-consuming task in onHandleIntent processing method. HandlerThread inheritance Handler, it is the same as the order of processing the message, which means IntentService also perform background task order.

Original: Big Box  Chapter 11 Android thread and thread pool


Guess you like

Origin www.cnblogs.com/chinatrump/p/11423910.html