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

标签 java android multithreading process

java.lang.Thread.setPriorityandroid.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/

相关文章:

java - 在 Web 应用程序中使用 React 和 JSX 类似于在 Android 中使用 Java 和 XML

multithreading - 双核cpu上的多线程

c - fork进程是C中多线程的一个例子吗?

java - 从带有 unicode 字符的 url 下载图像

java - Android Studio 使用 ObjectAnimation 制作动画

Java 正则表达式从字符串中拆分 double

java - 不是 org.apache.hadoop.mapreduce.Mapper

javascript - [Phonegap 3.4]phonegap.js 函数在除 index.html 之外的其他页面上不起作用

c# - 一个进程内可以共存多少个 'STA'线程?

java - Nexus 5 通知灯光颜色