Level above.

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

How do they differ?

Firstly, in the java.lang.Thread class

 java.lang.Thread.setPriority(int priority) 

priority can range from Thread.MIN_PRIORITY (= 1, the lowest) to Thread.MAX_PRIORITY (= 10, the 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; 

Secondly, in the class android.os.Process

 android.os.Process.setThreadPriority(int priority) 

priority can range from -20 (maximum) to 19 (lowest).

The android.os.Process class has related constants.

 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 do not know why. I do not know how they work differently.

+4
source share
2 answers

How do they differ?

Thread.setPriority only works for Java Thread objects; i.e. threads in the current JVM. In contrast, the javadoc for Process.setThreadPriority says it works for thread processes I. Since threads / processes are identified by tid values, they should not be in the current JVM.

The priority values ​​are also different, but this is less important. (In the case of Thread values ​​are mapped to the priorities of the main thread / processes of the platform. In the case of Process values ​​are the native priorities of the threads / processes ... of the Linux platform.)

I do not know how they work differently.

If you spot the differences above, they work the same. AFAIK, all modern standard JVMs rely on their own platform thread scheduler and prioritization mechanisms.

+5
source

The Android platform sometimes overrides some Java concepts to improve them on Android devices. You should always use special Android classes in Android development.

+1
source

Source: https://habr.com/ru/post/1483111/


All Articles