[TinyTut] - Change Thread Priority

Tutorials with advanced 'difficulty' and more Lines of Code.

[TinyTut] - Change Thread Priority

Postby plusminus » Tue Dec 18, 2007 10:49 pm

[align=center][TinyTut] - Change Thread Priority[/align]

What you learn: You will learn how to change the Thread-Priority of your Activity (Process). Possible field of use: Audio-Applications needing High-Priority Threads.

Difficulty: 2 of 5 :)

:idea: Questions/Problems: Simply post below...

What it will look like:
[align=center]Nothing to see here...[/align]

Description:
0.) To raise the Thread-Priority of our Threads in our Activity we need a 'uses-permission' in the AndroidManifest.xml to do so:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <uses-permission id="android.permission.RAISED_THREAD_PRIORITY"/>
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


So now you can change/raise the Priority of any Thread within your Activity using the following code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.os.Process;
  2.  
  3. // ...
  4.  
  5.  
  6.  
  7. // -----------------------------------
  8.  
  9. // Set the priority of the calling thread, based on Linux priorities:
  10.  
  11. // -----------------------------------
  12.  
  13.  
  14.  
  15. // Changes the Priority of the calling Thread!
  16.  
  17. Process.setThreadPriority(12);
  18.  
  19. // Changes the Priority of passed Thread (first param)
  20.  
  21. Process.setThreadPriority(Process.myTid(), 12);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

[align=center]:warning: Where the range is from -20 (high) to +19 (low). Choose not too high :warning: [/align]
:idea: The best is to use pre-defined constants from android.os.Process:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // Lower is 'more impotant'
  2.  
  3. Process.THREAD_PRIORITY_LOWEST = 19
  4.  
  5. Process.THREAD_PRIORITY_BACKGROUND = 5
  6.  
  7. Process.THREAD_PRIORITY_DEFAULT = 0
  8.  
  9. Process.THREAD_PRIORITY_FOREGROUND = -5
  10.  
  11. Process.THREAD_PRIORITY_DISPLAY = -10
  12.  
  13. Process.THREAD_PRIORITY_URGENT_DISPLAY = -15
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

:src: More detailed explanation on which Priority to use when.


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby regulatre » Sat Jan 09, 2010 3:29 pm

Does the Thread.setPriority work in the Android jvm? I'm actually starting my threads with Thread t = new Thread() {blah.......

And I can't find a way to use Process.setThreadPriority(tid,priority). But the Thread class does offer a setPriority(priority) method...

But according to this: http://www.javamex.com/tutorials/thread ... what.shtml
Thread priority can't be changed by non-root user, so it wouldn't work then in my app?
regulatre
Freshman
Freshman
 
Posts: 7
Joined: Thu Dec 03, 2009 1:33 am

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 8 guests