What's the difference between Thread.setPriority() and android.os.Process.setThreadPriority()
If I have code like:
Runnable r = ...;
Thread thread = new Thread(r);
thread.setPriority((Thread.MAX_PRIORITY + Thread.NORM_PRIORITY) / 2);
or ...
Runnable r = ...
Thread thread = new Thread( new Runnable() {
public void run() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_MORE_FAVORABLE);
r.run();
}
});
IS the android.os.Process way required/preferred?
WHY is the android.os.Process way preferred/required if it is?
This is not clearly documented as far as I can tell.
Solution 1:
The current Dalvik implementation seems to map Java Threads one by one to the underlying linux system PTHREADs like you say. All Threads of all apps belong to the same thread group on the system, so every Thread competes with all Threads of all apps.
So currently Thread.setPriority
should actually do the same thing as Process.setThreadPriority
, using the smaller Java priority scale. The mapping of priorities is defined in kNiceValues
at vm/Thread.c
Solution 2:
Google uses
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
in Volley's Network Dispatcher thread, so I would believe that using Process.setThreadPriority() is the way to go.
Solution 3:
I would rely on thread.setPriority()
. The android.os.Process.setThreadPriority
names a real thread priority of the underliying linux OS. However, those could, but doesn't need to map to Dalvik / Java VM threads, as the VM could do threading on its own means or use system threads or a combination of both. Raising the system priority would more likely result in prioritizing your application in favor of others, if not restricted by android security constraints, but not guarantee prioritizing your current Java Thread in favor of other Java Threads in your application.