java.lang.Thread.setPriority
和 android.os.Process.setThreadPriority
它们有什么不同?
首先,在java.lang.Thread
类中,
java.lang.Thread.setPriority(int priority)
priority
的值可以从 Thread.MIN_PRIORITY
(= 1,最低)到 Thread.MAX_PRIORITY
(= 10,最高)。
java.lang.Thread类中有相关常量。
public static final int MIN_PRIORITY = 1;
public static final int NORM_PRIORITY = 5;
public static final int MAX_PRIORITY = 10;
其次,在android.os.Process
类中,
android.os.Process.setThreadPriority(int priority)
priority
的值可以从 -20(最高)到 19(最低)。
android.os.Process类中有相关常量。
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;
我发现优先级值不一样,但不知道为什么。我不知道它们的工作方式有何不同。
最佳答案
How are they different?
Thread.setPriority
仅适用于 Java Thread 对象;即当前 JVM 中的线程。相比之下,Process.setThreadPriority
的 javadoc 表示它适用于线程和进程。由于线程/进程由 tid
值标识,因此它们不需要驻留在当前 JVM 中。
优先级值也不同,但意义不大。 (在 Thread
的情况下,值映射到平台的 native 线程/进程优先级。在 Process
的情况下,值是 native 的Linux 平台的线程/进程优先级。)
I don't know how they work differently.
如果您排除上述差异,它们的工作原理是相同的。据我所知,所有现代主流 JVM 都依赖于 native 平台线程调度程序及其优先级机制。
关于java.lang.Thread.setPriority 与 android.os.Process.setThreadPriority,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16788204/