Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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.

like image 858
Greg Giacovelli Avatar asked Mar 04 '11 19:03

Greg Giacovelli


People also ask

What is thread setPriority in Java?

The setPriority() method of thread class is used to change the thread's priority. Every thread has a priority which is represented by the integer number between 1 to 10. Thread class provides 3 constant properties: public static int MIN_PRIORITY: It is the maximum priority of a thread. The value of it is 1.

What is MAX_ Priority in Java?

MAX_PRIORITY : The maximum value is 10, kown as the maximum priority of a thread. NORM_PRIORITY : The normal value is 5, known as the normal priority of a thread.

How to change thread Priority?

The thread priority determines when the processor is provided to the thread as well as other resources. It can be changed using the method setPriority() of class Thread. There are three static variables for thread priority in Java i.e. MIN_PRIORITY, MAX_PRIORITY and NORM_PRIORITY.


2 Answers

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.

like image 87
dronus Avatar answered Oct 14 '22 03:10

dronus


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.

like image 32
Phileo99 Avatar answered Oct 14 '22 05:10

Phileo99