4

java.lang.Thread.setPriority and android.os.Process.setThreadPriority

How are they different?

First, in the java.lang.Thread class,

java.lang.Thread.setPriority(int priority)

priority can have a value from Thread.MIN_PRIORITY ( = 1, Lowest) to Thread.MAX_PRIORITY ( = 10, Highest).

There are related constants in the java.lang.Thread class.

public static final int MIN_PRIORITY = 1;
public static final int NORM_PRIORITY = 5;
public static final int MAX_PRIORITY = 10;

Second, in the android.os.Process class,

android.os.Process.setThreadPriority(int priority)

priority can have a value from -20 (Highest) to 19 (Lowest).

There are related constants in the android.os.Process class.

public static final int THREAD_PRIORITY_AUDIO = -16;
public static final int THREAD_PRIORITY_BACKGROUND = 10;
public static final int THREAD_PRIORITY_DEFAULT = 0;
public static final int THREAD_PRIORITY_DISPLAY = -4;
public static final int THREAD_PRIORITY_FOREGROUND = -2;
public static final int THREAD_PRIORITY_LESS_FAVORABLE = 1;
public static final int THREAD_PRIORITY_LOWEST = 19;
public static final int THREAD_PRIORITY_MORE_FAVORABLE = -1;
public static final int THREAD_PRIORITY_URGENT_AUDIO = -19;
public static final int THREAD_PRIORITY_URGENT_DISPLAY = -8;

I found that the priority values are different, but I don't know why. I don't know how they work differently.

4

2 回答 2

5

How are they different?

Thread.setPriority only works for the Java Thread objects; i.e. threads in the current JVM. By contrast, the javadoc for Process.setThreadPriority says it works for threads AND processes. Since the threads/processes are identified by tid values, they don't need to reside in the current JVM.

The priority values are different too, but that is less significant. (In the Thread case, the values are mapped to the platform's native thread/process priorities. In the Process case, the values are native the thread/process priorities ... of the Linux platform.)

I don't know how they work differently.

If you factor out the differences above, they work the same. AFAIK, all modern mainstream JVM's rely on the native platform thread scheduler and its prioritization mechanisms.

于 2013-05-28T09:35:44.063 回答
1

Android platform sometimes redefines some java concepts to improve them for Android running devices. You should always use the Android specific classes in Android developments.

于 2013-05-28T09:22:42.963 回答