Android thread category

Source: Internet
Author: User

Some time ago, I was developing an application collaboratively. I opened the final submitted project file and found the activity in it. The service component class accounts for only 40% of all class files, and the rest are classes of auxiliary components, A large part is the thread. When the Android Application starts, the main thread, namely the UI thread, is automatically opened. components such as activity and service run in the main thread. However, the main thread has strict restrictions on the response time. If it is stuck for more than 5 seconds, an ANR exception will be reported. Therefore, complicated time-consuming logic cannot be written in the main thread, this is why many threads exist in development, because many sub-threads are required to complete the business logic of the application.

Run the 'top-t' command in ADB to check the threads on the mobile phone. It is found that a large number of threads are generated in the background after each application is running and operated by the user, I have sorted out multiple application thread information, which is classified as follows:

Asynctask

Android asynchronous task class makes it easier for developers to feedback the background thread execution results to the UI. In the SDK description, asynctask is a helper class for thread and handler, there is no complete thread framework. The ideal use case is for brief background operations. When complicated and time-consuming operations exist, we recommend using executor, threadpoolexecutor, and futuretask. It does not have a complete thread framework. Why do I put it in the thread category? The reason is that asynctask is used as an independent Classification when I view the thread in the command line, as shown in.

2. executor

Based on the recommendation of the asynctask API, we can find it. Executor is a class in Java. Its implementation can contain the action of a thread or runnable thread. Executor itself is a framework of concurrent programming. A Programming Method of concurrent programming is to split tasks into small tasks in some columns, namely runnable, and then submit them to a executor for execution. Executor uses an internal thread pool to complete operations during execution. Executor provides an indirect layer between the client and the task execution. Unlike the client, this intermediary object executes the task. Executor allows developers to manage the execution of asynchronous tasks without displaying the lifecycle of Management threads.

After the experiment, the name of the thread that will be displayed in the background using executor in the program is (as shown in ):

Threadpoolexecutor

Threadpoolexecutor is a class in Java that implements an hread pool. Thread Pool is generally used to solve two problems: when processing a large number of synchronization tasks, it can avoid the overhead of thread creation and destruction. another, perhaps more important, means that, it actually represents a boundary. By using the thread pool, you can limit the resources consumed by these tasks, such as the maximum number of threads, such as the maximum message buffer pool.

The experiment shows that the threadpoolexecutor in the program is displayed in the background as follows:

Futuretask

Java class. The SDK explains it as follows: it is an asynchronous class that can be canceled; that is, it has start and cancel operations, in fact, the above three threads also have corresponding close operations, but some may not need to be managed by developers.

3timer

Timer is a timer that regularly triggers the corresponding action.

4. multi-thread File Download

This implementation process uses the basic Thread class, which is displayed in the background.

Five other threads (not manually started by developers)

Webviewcorethread: the thread automatically enabled by the application parsing HTML.

Cookiesyncmanager: Used to ensure data consistency between memory and hard disk. It is estimated that it is a thread automatically started when sharepreference is used.

Fileobserver: it is a class used to listen to files or folders. You can see that this class has an observerthread inside the android source code.

Refqueueworker: when the object reference is in the queue, the GC process starts this thread.

In addition to the threads directly or indirectly started by the above developers in the program, it also includes the built-in threads for DVM startup, because each application has a DVM, in the background, we found that all applications contain the following threads, which should be the built-in threads of DVM:

Recommended articles: http://www.iteye.com/topic/1118660

References: http://baike.baidu.com/view/2045577.htm

Http://blog.csdn.net/historyasamirror/article/details/5961368

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.