One, the main thread and child threads in Android
The main thread in Android can be thought of as a UI thread, not a time-consuming operation on the main thread, or it will give you a feeling of lag. The main thread is primarily used to process four components, and to handle their interactions with the user. The primary function of Anroid's child threads is to handle time-consuming operations.
You know, "after android3.0, the requirement for network access must be performed on the child thread, otherwise the networkonmainthreadexception exception will be thrown." ”
Second, the threading pattern in Android
The thread state in Android, in addition to the traditional thread, also contains Asynctask,handlerthread,intentservice.
Their bottom layer is actually thread, and they appear to simplify the process of updating the UI for a child thread.
Here is a description of their use and principle.
1,asynctask
In Asynctask, background tasks are processed in the thread pool, and the results of processing and progress of processing are passed to the main thread for easy updating of the UI.
Use it to easily perform background tasks and update the UI. It implements the principle that handler and thread, just asynctask encapsulate them.
publicabstractclass AsyncTask<Params, Progress, Result>
Asynctask is an abstract class, and its subclasses need to pass generics in.
Params:表示参数类型Progress:表示后台任务的进度类型Result:表示后台任务的返回值类型
It's method:
The following asynctask are used to access a picture on a network. The URL of the picture is a string, so its params is of type string. The progress type you need to update is a number, which is an integer type. There is no need to process the results of background tasks, so result is void type.
class asuleasynctask extends asynctask<String,Integer ,Void>{ the//asynchronous operation is executed before the call is run on the main thread @Override protectedvoid OnPreExecute () {Super. OnPreExecute (); LOG.I (Constants.LOG,"OnPreExecute"); }//Execute Asynchronous task, execute in thread pool, params represents input parameter of asynchronous task //In this method you can update the task progress bar by publishprogress (progress value) and call Onprogressupdate the results of the//background task are returned to OnPostExecute @Override protectedVoid doinbackground (String ... params) {log.i (Constants.LOG,"Doinbackground"); String url=params[0]; LOG.I (Constants.LOG,"url"+ URL);//Request picture according to URL, execute asynchronous task //........... return NULL; }//main thread run, update progress UI @Override protectedvoid Onprogressupdate (Integer ... values) {Super. onprogressupdate (values); }//asynchronous operations are called after execution, run on the main thread, //Its parameter is the return value of the Doinbackground method. Working with the results of background tasks @Override protectedvoid OnPostExecute (void aVoid) {Super. OnPostExecute (AVoid); LOG.I (Constants.LOG,"OnPostExecute"); } }
To perform a background task:
new AsuleAsyncTask().execute("http://f10.topitme.com/o/201102/17/12979229866129.jpg");
2, analyze the working principle of asynctask from the source code
Asynctask's Execute Method:
sDefaultExecutor实际上是一个串行的线程池。
AysncTask的onPreExecute方法会先执行,接下来线程池开始执行任务。FutureTask交给SerialExecutor来执行。
Thread pool execution: Mfuture is a variable of type Futuretask. Such as:Private FinalFuturetask<result> mfuture; It is initialized in the constructor method of Astnctask. Futuretask implements the Runnable interface, which can be regarded as the class of operation task, we all know that runnable implementation class can be understood as a task, thread is responsible for performing runnable task. Futuretask can be executed not only by threads as a task, but also by scheduling tasks. For example, the Cancel method of Futuretask can interrupt the thread executing the task and record the end state of the task. Futuretask can manage tasks relative to runnable. Mtasks is a arraydeque that stores runnable, which represents a task queue. The Execute method first inserts the Futuretask object into the Mtasks task queue. If there are no active asynctask tasks, the Schedulenext method is called. When a task is finished, Aysnctask will continue to perform other tasks until all the tasks have been executed. There are two thread pools in async, one is the following serialexecutor, and the other is the thread_pool_executor thread pool. Thread_pool_executor thread Pool: Public Static FinalExecutor Thread_pool_executor =NewThreadpoolexecutor (Core_pool_size, Maximum_pool_size, Keep_alive,timeunit.seconds, SP Oolworkqueue, sthreadfactory); Serialexecutor is used for the arrangement of tasks, while Thread_pool_executor is really used for processing tasks. (Schedulenext processing tasks)
处理任务时,会去执行FutureTask的run方法。AsyncTask的构造方法,对FutureTask进行了初始化,并且把WorkerRunnable实例作为FutureTask的构造参数。在FutureTask的run中会call方法,这里callable代表的就是WorkerRunnable对象。调用call方法,mTaskInvoked设为true,表示当前任务已经调用过了,然后执行AsyncTask的doInBackground方法,将其返回值传递给postResult方法。所以可以推断出doInBackground是运行在线程池中。
postResult会通过InternalHandler发送一个标记为MESSAGE_POST_RESULT的Message,InternalHandler在收到这个消息后,会调用AsyncTask的finish方法。InternalHandler的作用是把执行环境切换到主线程,所以InternalHandler的创建必须要是在主线程中。由于InternalHandler是静态的,AsyncTask类一加载就会进行初始化。同样,AsyncTask的类也必须要在主线程加载。而finish方法就很简单了,如果AsyncTask任务被取消了,就会调用onCancelled,如果没有,代表后台任务已经执行完毕,就会把返回结果交由onPostExecute方法。到这里,AsyncTask的整个工作过程就完毕了。
3,intentservice
IntentService继承自Service,并且它是抽象类,创建它的子类实现抽象方法才可以使用它。IntentService负责执行后台的耗时的任务,也因为IntentService是服务的原因,这导致它的优先级比单纯的线程要高。所以IntentService适用于执行高优先级的后台任务。
In the OnCreate of Intentservice:
@Override publicvoidonCreate() { super.onCreate(); new HandlerThread("IntentService[""]"); thread.start(); mServiceLooper = thread.getLooper(); new ServiceHandler(mServiceLooper); }
HandlerthreadThread = NewHandlerthread ("intentservice[" +Mname+ "]");Thread.Start (); Handlerthread inherits fromThreadRepresents a thread, and the Start method calls Handlerthread's run. As follows: Public voidRun () {Mtid=Process.Mytid (); Looper.Prepare (); Synchronized (this) {Mlooper=Looper.Mylooper (); Notifyall (); } Process.SetThreadPriority (mpriority); Onlooperprepared (); Looper.Loop(); Mtid= -1; }looper.The Prepare method creates Message Queuing MessageQueue and Looper objects. Looper.Loop(); Starts polling messages in the message queue. Mservicelooper= Thread.Getlooper (); Mservicehandler= NewServicehandler (Mservicelooper); Constructs a handler object Servicehandler, based on the obtained looper. You know, the prepare method is called by the Main method of Activitythread, which means that the MessageQueue and Looper objects are actually created when the application is started. Here again for the reason, I think so, Activitythread's main created is a representative of the main thread of the MessageQueue and Looper. And here we are creating MessageQueue and Looper objects for a child thread. The MessageQueue and Looper objects are the basis for building handler. So it is assumed that the messages and processing messages sent by Servicehandler are in the Handlerthread thread.
Each time you start Intentservice, in addition to OnCreate initialization, Intentservice handles the intent of background tasks in Onstartcommand. Public int Onstartcommand(Intent Intent,intFlagsintStartid) {OnStart (intent, Startid);returnMredelivery? Start_redeliver_intent:start_not_sticky;} Public void OnStart(Intent Intent,intStartid) {Message msg = Mservicehandler.obtainmessage (); MSG.ARG1 = Startid; Msg.obj = Intent; Mservicehandler.sendmessage (msg);}Private Final class servicehandler extends Handler { Public Servicehandler(Looper Looper) {Super(Looper); }@Override Public void Handlemessage(Message msg) {onhandleintent (Intent) msg.obj); Stopself (MSG.ARG1); In the}}onstartcommand method, Mservicehandler sends a message that will be processed in Servicehandler. How to deal with it? Msg.obj represents the intent object that you open Intentservice, this time you can parse out the external parameters from the intent object, through which you can distinguish the specific background tasks. After Onhandleintent executes, stopself (taskId) waits for all messages to complete before terminating the service. To determine that all messages have been executed: the number of times the service was recently started and whether the Startid is equal, equal to assume that all messages have been executed, or the service will not be terminated. Execution order problem: Perform a background task to open a intentservice, while the internal is sent through handler message processing, and Looper polling messages are sequential, so when there are multiple background tasks, will be executed in the order of the outside.
Example:
class myintentservice extends intentservice{ PublicMyintentservice (String name) {Super(name); } @Overrideprotected voidOnhandleintent (Intent Intent) {String Name=intent.getstringextra ("Football"); LOG.I (Constants.log,name); } @Override Public voidOnDestroy () {Super. OnDestroy (); LOG.I (Constants.LOG,"OnDestroy"); }} Intent service=NewIntent ( This, Myintentservice.class); Service.putextra ("Football","Ronaldo"); StartService (service); Service.putextra ("Football","Messi"); StartService (service);
Thread pool in 4,android
Reason for the thread pool to appear:
1,可以重用线程池中的线程,避免因为线程的开启和销毁带来的性能开销。2,可以控制线程池的最大并发数,避免因为大量线程抢占系统资源而导致阻塞。3,可以对线程进行管理,比如提供了定时执行和间隔循环执行的功能。
The thread pool in Android comes from Java's executor, which is an interface, and the real implementation is the Threadpoolexecutor class.
Several thread pools in Android, the bottom layer are configured by Threadpoolexecutor.
Here's a look at Threadpoolexecutor.
Threadpoolexecutor has a number of constructors that configure the thread pool we want by specifying these parameters. Public threadpoolexecutor (int corepoolsize, int maximumpoolsi Ze, long keepalivetime, timeunit unit, B lockingqueue<runnable> workQueue threadfactory threadfactory)corepoolsize:The number of thread pools in the core, by default threads in the core thread pool will survive, even when idlemaximumpoolsize:The maximum number of threads the thread pool can hold, after which the number of activities exceeds this value, subsequent new tasks will be blockedKeepAliveTime:Non-core thread idle timeout length, longer than this time, non-core thread will be recycled when Threadpoolexecutor's allowcorethreadtimeout is set to true, it is also used for core threadsUnit:The unit of time used to specify KeepAliveTime, which is an enumerationWorkQueue:The thread pool's task queue, the Runnable object that is submitted through the thread pool's Execute method, is stored in this parameterthreadfactory:Thread Factory, which provides the thread pool with the ability to create new threads, Threadfactory is an interface with only one method: Threadfactory. Newthread
Common thread pools in Android:
By looking at its construction methods, you can know the functional characteristics of each thread pool according to the Threadpoolexecutor parameter information.
Create Fixedthreadpoolexecutors.newfixedthreadpool (nThreads); public static executorservice newfixedthreadpool (int nthreads) {return new threadpoolexecutor (nThreads, NTHR EADS, 0 L, Timeunit.milliseconds, new Linkedblockingqueue<runnable> ());} It is a thread pool with a fixed number of threads, and threads are core threads. Because they are core threads, all threads are not recycled when they are idle. When all threads are active, there are no idle threads, and if a new task comes over, it will be in a wait state. Only the core thread, and not recycled, means that fixedthreadpool can respond more quickly to outside requests.
Create Cachedthreadpoolexecutors.newcachedthreadpool () public static executorservice Newcachedthreadpool () {return new Thre Adpoolexecutor (0 , Integer.max_value, 60 L, timeunit.seconds, new SYNCHR Onousqueue<runnable> ());} All threads are non-core threads, and there is no upper limit on the number of non-core threads. When all threads are active, new threads are created to perform new tasks if new tasks come up. Timeunit is 60 seconds, and when idle threads exceed 60 seconds or idle, they will be recycled. Because it is recycled, it hardly consumes system resources and is suitable for performing a large number of less time-consuming tasks.
create Scheduledthreadpoolexecutors.newscheduledthreadpool () public static scheduledexecutorservice newscheduledthreadpool (int corepoolsize) {return new scheduledthreadpoolexecutor (corepoolsize);} public scheduledthreadpoolexecutor (int corepoolsize) {super (Corepoolsize, Integer.max_value, Default_keepalive_millis, M Illiseconds, new delayedworkqueue ());} Default_keepalive_millis=10 l core threads are fixed and there is no limit on the number of non-core threads. and non-core threads are quickly recycled if they are idle. Applies to recurring tasks that perform scheduled tasks and have a fixed period.
创建SingledThreadPoolExecutors.newSingleThreadExecutor()publicstaticnewSingleThreadExecutor() { returnnew FinalizableDelegatedExecutorService (new ThreadPoolExecutor(11, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()));}核心线程有且只有一个,并且不会被回收。它确保所有的任务都在同一个线程中按顺序执行。它可以统一所有的外界任务在同一个线程中按顺序执行,使得这些线程之间不需要解决线程同步的问题。
Asynctask,intentservice working principle Analysis &android thread pool