1. 程式人生 > 其它 >Android 執行緒排程

Android 執行緒排程

一、執行緒排程方案

  • 執行緒優先順序nice值。
  • cgroup執行緒分組策略。

二、執行緒優先順序nice值

    • nice值是在Process類中定義的。
    • nice值越小,優先順序越高。
    • THREAD_PRIORITY_DEFAULT = 0。
    • 執行緒優先順序具有繼承性。

  Android中UI執行緒的優先順序是TTHREAD_PRIORITY_DEFAULT=0,在Android中執行緒還有哪些優先順序:

public static final int THREAD_PRIORITY_LOWEST = 19;

/**
 * Standard priority background threads.  This gives your thread a slightly
 * lower than normal priority, so that it will have less chance of impacting
 * the responsiveness of the user interface.
 * Use with {
@link #setThreadPriority(int)} and * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal * {@link java.lang.Thread} class. */ public static final int THREAD_PRIORITY_BACKGROUND = 10; /** * Standard priority of threads that are currently running a user interface * that the user is interacting with. Applications can not normally * change to this priority; the system will automatically adjust your * application threads as the user moves through the UI. * Use with {
@link #setThreadPriority(int)} and * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal * {@link java.lang.Thread} class. */ public static final int THREAD_PRIORITY_FOREGROUND = -2; /** * Standard priority of system display threads, involved in updating * the user interface. Applications can not * normally change to this priority. * Use with {
@link #setThreadPriority(int)} and * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal * {@link java.lang.Thread} class. */ public static final int THREAD_PRIORITY_DISPLAY = -4; /** * Standard priority of the most important display threads, for compositing * the screen and retrieving input events. Applications can not normally * change to this priority. * Use with {@link #setThreadPriority(int)} and * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal * {@link java.lang.Thread} class. */ public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8; /** * Standard priority of video threads. Applications can not normally * change to this priority. * Use with {@link #setThreadPriority(int)} and * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal * {@link java.lang.Thread} class. */ public static final int THREAD_PRIORITY_VIDEO = -10; /** * Standard priority of audio threads. Applications can not normally * change to this priority. * Use with {@link #setThreadPriority(int)} and * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal * {@link java.lang.Thread} class. */ public static final int THREAD_PRIORITY_AUDIO = -16; /** * Standard priority of the most important audio threads. * Applications can not normally change to this priority. * Use with {@link #setThreadPriority(int)} and * {@link #setThreadPriority(int, int)}, <b>not</b> with the normal * {@link java.lang.Thread} class. */ public static final int THREAD_PRIORITY_URGENT_AUDIO = -19; /** * Minimum increment to make a priority more favorable. */ public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1; /** * Minimum increment to make a priority less favorable. */ public static final int THREAD_PRIORITY_LESS_FAVORABLE = +1;

三、CGroup

  即使,執行緒使用nice值優先順序策略,在多執行緒環境下,後臺執行緒執行緒的CPU佔用時間總和,可能比前臺執行緒佔用CPU時間多。所以,Android除了執行緒優先順序策略,還有CGroup,嚴格模式的執行緒群組策略。

    • CGroup採用嚴格的群組排程策略。
    • 群組策略保證Android前臺執行緒能夠獲得跟多的CPU時間。

  Android中定義哪些執行緒群組:

/**
 * Default thread group -
 * has meaning with setProcessGroup() only, cannot be used with setThreadGroup().
 * When used with setProcessGroup(), the group of each thread in the process
 * is conditionally changed based on that thread's current priority, as follows:
 * threads with priority numerically less than THREAD_PRIORITY_BACKGROUND
 * are moved to foreground thread group.  All other threads are left unchanged.
 * @hide
 */
public static final int THREAD_GROUP_DEFAULT = -1;

/**
 * Background thread group - All threads in
 * this group are scheduled with a reduced share of the CPU.
 * Value is same as constant SP_BACKGROUND of enum SchedPolicy.
 * @hide
 */
public static final int THREAD_GROUP_BACKGROUND = 0;

/**
 * Foreground thread group - All threads in
 * this group are scheduled with a normal share of the CPU.
 * Value is same as constant SP_FOREGROUND of enum SchedPolicy.
 * Not used at this level.
 * @hide
 **/
private static final int THREAD_GROUP_FOREGROUND = 1;

/**
 * System thread group.
 * @hide
 **/
public static final int THREAD_GROUP_SYSTEM = 2;

/**
 * Application audio thread group.
 * @hide
 **/
public static final int THREAD_GROUP_AUDIO_APP = 3;

/**
 * System audio thread group.
 * @hide
 **/
public static final int THREAD_GROUP_AUDIO_SYS = 4;

/**
 * Thread group for top foreground app.
 * @hide
 **/
public static final int THREAD_GROUP_TOP_APP = 5;

/**
 * Thread group for RT app.
 * @hide
 **/
public static final int THREAD_GROUP_RT_APP = 6;

/**
 * Thread group for bound foreground services that should
 * have additional CPU restrictions during screen off
 * @hide
 **/
public static final int THREAD_GROUP_RESTRICTED = 7;

四、總結

  1. 執行緒優先順序具有繼承性。
  2. Android中執行緒排程策略開發者只能調整nice值,即執行緒優先順序。
  3. Android中執行緒優先順序nice值越小,優先順序越高。nice值不像Java執行緒優先順序只能設定1~10,Android執行緒優先順序可以設定負數。
  4. Android執行緒排程策略還採用了嚴格的群組排程策略,保證Android在多執行緒環境下,前臺執行緒有足夠的CPU時間。
  5. Android中,開發只能修改執行緒的優先順序nice值。