一 问题思考
安卓开发,如果想设置线程优先级有两种方法:
1 Android sdk也提供一个设置线程优先级的方法
2 Thread.java里面提供了设置线程优先级的方法
这两个方法有什么区别,应该选择使用哪一个呢?
二 线程优先级的原理
2.1. android.os.process.java设置线程优先级源码分析
/**
* 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;
注释的意思大致是这里设置优先级是基于linux线程优先级的实现。这是一个jni方法,对应实现文件在android_util_process.cpp文件:
tatic const JNINativeMethod methods[] = {
{
"getUidForName", "(Ljava/lang/String;)I", (void*)android_os_Process_getUidForName},
{
"getGidForName", "(Ljava/lang/String;)I", (void*)android_os_Process_getGidForName},
{
"setThreadPriority", "(II)V", (void*)android_os_Pr