Processes and Threads
In general, Android opens a process for an application to execute, with all the components in the application executing through a separate thread, all of which thread that shares the resources of the application process. When an application is started, the Android system launches a new Linux application process and an execution thread. By default, all components in an application run are running in the same process and thread, where the threads are generally referred to as the main thread. If an application's component starts with a process already in existence, the application starts the component in the same execution thread as it does.
Process
By default, all components under the same application run in the same process, and most applications should not change this. However, if you need to control which process belongs to that component, you can configure it in the Androidmanifest.xml file. In general, component elements:<activity>, <service>, <receiver>, <provider> all support a android:process property, You can set this property to allow different components to run separately in their own processes, or you can use this property to run different application components in the same process and share the same Linux User ID and give the same certificate.
The tips:<application> element also supports the Android:process property, which is used to set all components.
Android, in the case of low memory, shuts down some of the lower priority processes to increase the more important process of memory running, while all threads in the process are destroyed at the same time. With enough memory, the Android system will view the application process as much as possible to achieve a quick start to the next run, but eventually it will need to remove the old process and reclaim the memory for new or more important processes. By prioritizing processes to determine whether they are recycled, a lower-priority process is typically recycled to make resources available for high-priority processes.
Here are five types of Android processes, their priority order:
- Foreground process: foreground progress.
- Visible prcess: Visible process.
- Service process: Servicing processes.
- Background process: Background processes.
- Empty process: Empty processes.
Tips: The priority of a process can vary.
Thread
When an application is started, the system creates an execution thread in the process of the application, commonly referred to as the "main thread". This thread is important because it is responsible for distributing events to the user component of the response, including drawing events, so the main thread is called the UI thread. The system does not create a separate thread for each component, but rather completes the initialization of these components in the UI thread, so the system callback method is run in the UI thread, such as the Click event.
When a program performs more complex work to respond to user interaction, even if the application is properly executed, single-threaded mode can cause poor performance. For example, if all of the application functions occur in the UI thread, when time-consuming operations are performed, such as accessing the network or querying data, blocking the UI first, causing other events to not be distributed to the event queue, including screen-drawing events. Causes the application to die from the user's point of view. In Android, when a UI thread is blocked for more than a few seconds (about 5 seconds), a dialog box pops up that has no response from the application, causing the user experience to be poor and may force the user to opt out of your application or simply uninstall it directly.
In addition, all components under the Android UI Toolkit package are not thread-safe, so you cannot manipulate these UI components in a separate worker thread, and you must manipulate them in the UI thread. Therefore, for a single-threaded model, Android has two rules:
- Cannot block UI thread
- Components under the Android UI Toolkit package cannot be accessed in a worker thread.
For time-consuming operations, you should put them in a separate thread. For example: The following clicks the event via a Demo listener button, downloading a picture from a separate thread and displaying it in a imageview.
1 btnerror2.setonclicklistener (new View.onclicklistener () {
2 @Override
3 public void OnClick ( View v {
4 //Add one thread to access network
5 new Thread (new Runnable () {
6 @Override 7 public void Run () {
8 //Get the picture under the address
9 Bitmap btm=loadimagefromnetwork
("http://ww4.sinaimg.cn/bmiddle/ 786013a5jw1e7akotp4bcj20c80i3aao.jpg "); Imageview1.setimagebitmap (BTM); ). Start ();
} );
At first, it seems reasonable to start a new thread to access the network, but it violates rule two, cannot modify the UI component outside of the Android UI main thread, while in click New Thread is a worker thread, the UI component cannot be manipulated in the worker thread, the above demo will complain.
To fix the above error, Android offers several ways to access the UI thread from another thread:
- Activity.runonuithread (Runnable): Runs on the specified UI thread, executes immediately if the current thread is a UI thread, and publishes to the UI thread's event queue if the current thread is not a UI thread.
- View.post (Runnable): Publishes an event to the UI thread and is executed immediately.
- View.postdelayed (Runnanle,long): Publishes an event to the UI thread, the delay is performed, and the number of delays is the passed long parameter.
The following two Dem is used to manipulate the UI components through the methods described above:
Activity.runonuithread:
1 btnrunonuithread.setonclicklistener (new View.onclicklistener () {
2
3 @Override
4 public void OnClick (View v) {
5 new Thread (new Runnable () {
6
7 @Override
8 Public void Run () {
9 final Bitmap btm=loadimagefromnetwork
("http://ww4.sinaimg.cn/bmiddle/ 786013a5jw1e7akotp4bcj20c80i3aao.jpg "); MainActivity.this.runOnUiThread (New Runnable () {One
@Override public void Run () { Imageview1.setimagebitmap (BTM);
} );
} ). Start ();
} );
Effect Demo:
View.post
%201%20%20%20%20%20%20%20%20%20btnpost.setonclicklistener (New%20view.onclicklistener ()%20{%202%20%20%20% 20%20%20%20%20%20%20%20%20%20%203%20%20%20%20%20%20%20%20%20%20%20%20%20@override%204%20%20%20%20%20%20%20%20% 20%20%20%20%20public%20void%20onclick (view%20v)%20{%205%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20new% 20Thread (new%20runnable ()%20{%206%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%207%20%20%20% 20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20@override%208%20%20%20%20%20%20%20%20%20%20%20%20%20%20% 20%20%20%20%20%20%20public%20void%20run ()%20{%209%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20% 20%20%20%20%20final%20bitmap%20btm=loadimagefromnetwork ("http://ww1.sinaimg.cn/bmiddle/
88ff29e8jw1e7pjnpfxbrj20dp0a90tb.jpg "); Imageview1.post (New Runnable () {11 12 @Override public void Run () {14 TODO auto-generated Method Stub imageview1.setimagebitmap (BTM);
16} 17});
}). Start (); });
Effect Demo: