Android process and thread description 2: Thread

Source: Internet
Author: User
Thread

When an application is started, the system creates an execution thread called "Main ". This thread is very important because it is in charge of dispatching events to user interface controls. This includes drawing events. It is also your application and interface Toolkit (Android. widget and
Components in the Android. View package. Therefore, the main thread is also called an interface thread.

The system does not create threads for each component instance. All components running in a process are instantiated in the interface thread, and the system calls each component are distributed in this thread. As a result, methods that respond to system calls (such as onkeydown () reporting user actions or a lifecycle callback method) are always processed in the interface thread.

For example, when you touch a button on the screen, the interface thread of your application distributes the touch event to the control, then, the control sets its press status and sends a request to the event queue that becomes invalid on its own interface. The interface thread retrieves the request from the queue and notifies the control to repaint itself.

When your application needs to perform a large number of operations in response to user interaction, this single-threaded mode will bring low performance, unless you can properly optimize your program. In particular, if everything happens in the interface thread and the time-consuming operations such as network connection or database requests are executed, the response of the whole interface will be blocked. When a thread is blocked, events, including drawing events, cannot be distributed. From the user's point of view, the program response is too slow. Even worse, if the interface thread is blocked for several seconds (5 seconds in size), the user complained that the program did not respond, and the user may quit and delete your application.

In addition, the andoid interface is NOT thread-safe. Therefore, you must not operate your interface in a work thread-you can only manage your interface in the interface thread. Therefore, there are two simple rules for the single-thread mode:

1
Do not block interface threads

2Do not operate the interface outside the interface thread.

Worker thread

Because of the single-thread mode above, it is very important not to block your interface thread to keep the response on your application interface. If you have tasks that cannot be completed quickly, you should put them in another thread for execution (background thread or working thread ).

For example, the following code is used to respond to the click event. Download an image from another thread and display it in an imageview:

Public void onclick (view v) {<br/> New thread (New runnable () {<br/> Public void run () {<br/> bitmap B = loadimagefromnetwork ("http://example.com/image.png"); <br/> mimageview. setimagebitmap (B); <br/>}< br/> }). start (); <br/>}
At first glance, this looks good because it creates a new thread for network operations. However, it violates the second rule: do not operate the interface outside the interface thread-it simply modifies the imageview in the work thread. This leads to an undefined exception and is difficult to debug and track.

To correct this problem, Android provides many methods to operate the interface from other threads. The following are available methods:

1 activity. runonuithread (runnable)

2 view. Post (runnable)

3 view. postdelayed (runnable, long)

For example, you can use view. Post (runnable) to fix the above problem:

Public void onclick (view v) {<br/> New thread (New runnable () {<br/> Public void run () {<br/> final Bitmap bitmap = loadimagefromnetwork ("http://example.com/image.png"); <br/> mimageview. post (New runnable () {<br/> Public void run () {<br/> mimageview. setimagebitmap (Bitmap); <br/>}< br/>}); <br/>}< br/> }). start (); <br/>}
Now this implementation is thread-safe: network operations are in another thread and imageview
Changes in the interface thread.

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.