Android process priority

Source: Internet
Author: User

[This article is my translation and summary of processes and threads in Dev guide, and I have added some personal understandings.]

 

Processes in Android

By default, all components in the same application run in the same Linux Process. when you start a component A, if there is already a component B in the running state and a and B belong to the same application, component A will run in the process where component B is located. otherwise, a new Linux Process will be created for.

You can also specify different running processes for component in the application. manifest. in the XML file, the <activity>, <service>, <Cycler>, and <provider> labels all support the Android: process attribute. With this attribute, you can specify the running process for component. <Application> the tag also supports setting the Android: process attribute to specify the default running process for all components under the application.

 

Process Priority

When the system memory is insufficient, the android system will choose to kill some less important processes based on the process priority. The process priority ranges from high to low:

1. Foreground process. The following processes are foreground processes:

A. The process contains a foreground activity that is interacting with the user;

B. The process contains the service bound to the foreground activity;

C. The process contains the service that calls the startforeground () method;

D. The process contains services that are executing the oncreate (), onstart (), or ondestroy () method;

E. The process contains the broadcastreceiver that is executing the onreceive () method.

The number of foreground processes in the system is very small, and the foreground processes are almost never killed. Only when the memory is low to ensure that all foreground processes run simultaneously will they choose to kill a foreground process.

2. Visible process. The following processes are visible processes:

A. the process contains the activity that is not in the foreground but still visible (the onpause () method of the activity is called, but the onstop () method is not called ). A dialog box is displayed when you run the activity. Although the activity is not a foreground activity, it is still visible.

B. The process contains the service bound to the visible activity.

The visible process will not be killed by the system, unless it is necessary to ensure the running of the foreground process.

3. service process. The process contains the started service.

4. background process. the process contains an invisible activity (the activity after the onstop () method is called ). the background process does not directly affect the user experience. To ensure that the foreground process/visible process/service process runs, the system may kill a background process at any time. an activity that correctly implements the lifecycle method is killed by the system when it is in the background. It can be restored when the user restarts it.

5. empty Process. any active process is an empty process. the system often kills empty processes without any impact. the only reason for the existence of a null process is to cache startup data so that it can be started faster next time.

 

Additional description of Process Priority

1. The system will give the process the highest priority possible. For example, if a process contains both the started service and the foreground activity, the process will be considered as the foreground process.

2. because of the dependency between components, the priority of processes may be increased. if process a serves process B, the priority of process a cannot be lower than process B. for example, if the contentprovider component of process a is serving a component of process B, or the service component of process a is bound to a component of process B, process A has a higher priority than process B (if the priority of process a is indeed lower than process B according to the priority rules, the system will choose to increase the priority of process a to be the same as process B ).

3. because the service process has a higher priority than the background process, if the activity needs to perform time-consuming operations, it is best to start a service to complete. of course, the time-consuming operations can be performed by the promoter thread in the activity, but the disadvantage of this operation is that once the activity is no longer visible, the process of the activity becomes the background process, when the memory is insufficient, background processes may be killed by the system at any time (however, the time-consuming operations after the service is started may cause data interaction problems. For example, if time-consuming operations require real-time updating of the UI control status, service is not a good choice ). for the same reason, time-consuming operations should not be performed in broadcastreceiver,
Instead, you should start the service to complete it (of course, the life cycle of broadcastreceiver is too short, and it also determines that time-consuming operations cannot be performed in it ).

 

Threads in Android

The system does not start a new thread for every component in the process, and all components in the process are instantiated in the UI thread. For more information about the multithreading mechanism in Android, see my another blog http://coolxing.iteye.com/blog/1208371

Always remember:

1. Do not block the UI thread. If the UI thread is blocked or time-consuming, the UI thread cannot respond to user requests.

2. You cannot update the UI in a non-UI thread (also known as a working thread) Because Android UI controls are thread-insecure.

As described above, developers often start working threads to complete time-consuming or blocking operations. If you need to update the UI status during the execution of the working thread, you should notify the UI thread to do so.

 

Inter-thread Communication

See the following code:

Java code
  1. Public void onclick (view v ){
  2. New thread (New runnable (){
  3. Public void run (){
  4. Bitmap B = loadimagefromnetwork ("http://example.com/image.png ");
  5. Mimageview. setimagebitmap (B );
  6. }
  7. }). Start ();
  8. }

The above code is incorrect. mimageview. setimagebitmap (B) violates the second criterion-the UI cannot be updated in the work thread.

Inter-thread communication can solve the problem of how the worker thread notifies the UI thread to update the control. Android provides three inter-thread communication solutions:

1. Call the following methods:

Activity. runonuithread (runnable)

View. Post (runnable)

View. postdelayed (runnable, long)

If the three methods are called in the working thread, the runnable parameter encapsulation operation in the method will be executed in the UI thread.

This method can be used to correct errors in the example:

Java code
  1. Public void onclick (view v ){
  2. New thread (New runnable (){
  3. Public void run (){
  4. Final Bitmap bitmap = loadimagefromnetwork ("http://example.com/image.png ");
  5. Mimageview. Post (New runnable (){
  6. // The run method will be executed in the UI thread
  7. Public void run (){
  8. Mimageview. setimagebitmap (Bitmap );
  9. }
  10. });
  11. }
  12. }). Start ();
  13. }

2. handler mechanism. the handler mechanism allows developers to call the sendmessage () method of the handler object bound to the UI thread in the working thread to send a message to the Message Queue of the UI thread, the UI thread extracts messages from the message queue and completes processing as appropriate.

3. use asynctask class. create a subclass of the asynctask class and overwrite the onpreexecute (), doinbackground (), onprogressupdate (), onpostexecute () methods as needed. for more information about how to use the asynctask class, see the document:

A. the asynctask class is a generic class with three generic parameters. the first parameter specifies the parameter type of the execute method, the second parameter specifies the parameter type of the onprogressupdate () method, and the third parameter specifies the return value type of the doinbackground () method and onpostexecute () the parameter type of the method.

B. execution Process: Call the execute method of the asynctask class in the UI thread (only this step is controlled by the programmer) --> the system calls onpreexecute (), this method is executed in the UI thread --> the system calls the doinbackground () method, which is executed in the work thread --> the publishprogress () method is called every time in the doinbackground () method, after the onprogressupdate () method --> doinbackground () method is executed in the UI thread, the system calls the onpostexecute () method,
The return value of doinbackground () method is passed to the form parameter of onpostexecute () method. onpostexecute () method is executed in the UI thread.

This method can also be used to correct errors in the example:

Java code
  1. Public void onclick (view v ){
  2. New downloadimagetask(cmd.exe cute ("http://example.com/image.png ");
  3. }
  4. Private class downloadimagetask extends asynctask <string, void, bitmap> {
  5. Protected bitmap doinbackground (string... URLs ){
  6. Return loadimagefromnetwork (URLs [0]);
  7. }
  8. Protected void onpostexecute (Bitmap result ){
  9. Mimageview. setimagebitmap (result );
  10. }
  11. }

Related Article

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.