What's the difference between Thread.setPriority() and android.os.Process.setThreadPriority()

Android

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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 - Android

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 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGreg GiacovelliView Question on Stackoverflow
Solution 1 - AndroiddronusView Answer on Stackoverflow
Solution 2 - AndroidPhileo99View Answer on Stackoverflow
Solution 3 - AndroiddronusView Answer on Stackoverflow