事实证明,线程的优先级是影响线程的调度的主要因素。其他因素包括占用CPU的执行时间长短,线程分组调度等等。
另外,有两种方法可以设置线程的优先级。
android.os.Process
Process.setThreadPriority(int priority); // Calling thread.
Process.setThreadPriority(int threadId, int priority); // Thread with specific id.
通过这种方式设置的优先级,又称“友好值”,“友好值”越高,优先级越低。
范围是[-20, 19],优先递减。相关源码如下:
/**
* Standard priority of application threads.
* 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_DEFAULT = 0;
/*
* ***************************************
* ** Keep in sync with utils/threads.h **
* ***************************************
*/
/**
* Lowest available thread priority. Only for those who really, really
* don't want to run if anything else is happening.
* 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_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 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;
/**
* Set the priority of the calling thread, based on Linux priorities. See
* {@link #setThreadPriority(int, int)} for more information.
*
* @param priority A Linux priority level, from -20 for highest scheduling
* priority to 19 for lowest scheduling priority.
*
* @throws IllegalArgumentException Throws IllegalArgumentException if
* <var>tid</var> does not exist.
* @throws SecurityException Throws SecurityException if your process does
* not have permission to modify the given thread, or to use the given
* priority.
*
* @see #setThreadPriority(int, int)
*/
public static final native void setThreadPriority(int priority)
throws IllegalArgumentException, SecurityException;
/**
* Set the priority of a thread, based on Linux priorities.
*
* @param tid The identifier of the thread/process to change.
* @param priority A Linux priority level, from -20 for highest scheduling
* priority to 19 for lowest scheduling priority.
*
* @throws IllegalArgumentException Throws IllegalArgumentException if
* <var>tid</var> does not exist.
* @throws SecurityException Throws SecurityException if your process does
* not have permission to modify the given thread, or to use the given
* priority.
*/
public static final native void setThreadPriority(int tid, int priority)
throws IllegalArgumentException, SecurityException;
java.lang.Thread
setPriority(int priority);
范围是[1, 10],优先级递增。
参考资料
http://developer.android.com/reference/java/lang/Thread.html
http://developer.android.com/reference/android/os/Process.html