"Android Application Performance Optimization" chapter 5th Multithreading and synchronization

Source: Internet
Author: User

1. System threads visible in Ddms (Andorid3.1 Galaxy Tab 10.1 for example):

Main

Heapworker performing finalize functions and referencing object cleanup

GC Garbage collector garbage Collection

Signal Catcher captures Linux signals for processing

JDWP Java Debug Wire Protocol Debug Protocol Service

Compiler JIT Compiler Instant Compiler

Binder Thread #1 Binder Communication

Binder Thread #2

2. Thread

The thread object, which is an instance of the Java-Defined tread class. is the execution unit with the call stack itself

Typical error: Call run () instead of start () This will cause the run () method of the thread/or Runnable object to be invoked in the current thread. No new threads generated

3. How to change thread priority

(1) Thread priority from 1 to 10

Thread.setpriority () can

New Thread ("thread name") {@override  publicvoid  Runnable () {} }thread.setpriority (thread.max_priority); // highest priority (occurrence than UI line) Thread.Start ();

Min_priority (1)

Norm_priority (5)--default priority, if no priority is specified, default priority is used

Max_priority (10)

Ilegalargumentexception exception is thrown if the thread priority of the app setting exceeds the range of values (1-10)

(2) Another way is to use the process.setthreadpriority () in the Android.os bag

It is based on the priority of Linux, from 20 (highest) to 19 (lowest)

8 priority levels are defined:

Thread_priority_audio (-16)

Thread_priority_background (10)

Thread_priority_default (0)

Thread_priority_display (-4)

Thread_priority_foreground (-2)

Thread_priority_lowest (19)

Thread_priority_urgent_audio (-19)

Thread_priority_urgent_ DISPLAY (-8)

You can also use Process.thread_priority_less_favorable (+1) and process.thread_priority_less_favorable (-1)

If you want to set the thread priority higher than the default. Then you can use (Thread_priority_default + thread_priority_less_favorable)

In this way, instead of you direct + 1,-1, is because +1 sometimes means to increase the priority, sometimes it is to lower the priority level.

Note: Do not mix thread.setpriority and process.setthreadpriority. Because thread and Linux have different priority definitions.

Note: Changing the thread priority must be very careful. Increasing the priority may speed up the character execution of this thread, but it can negatively affect other threads and affect the overall experience.

The priority aging algorithm can be considered.

Again: The background thread updates the user interface and must be fed back to the main thread, and the view method can only be called in the UI thread.

"Android Application Performance Optimization" chapter 5th Multithreading and synchronization

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.