android 改变线程优先级Change Thread Priority

来源:互联网 发布:chrome 页面调试js 编辑:程序博客网 时间:2024/05/01 03:14

  为了提高 我们的Activity中的线程的

线程优先级(Thread-Priority),我们需要在AndroidManifest.xml 中使用 'uses-permission' 这样做:
XML: 
          <uses-permission id="android.permission.RAISED_THREAD_PRIORITY"/>

  现在你可以在你的Activity中使用以下代码改变或提高任何线程的优先级:
Java: 
          import android.os.Process;
// ...

// -----------------------------------
// Set the priority of the calling thread, based on Linux priorities:
// -----------------------------------

// Changes the Priority of the calling Thread!
Process.setThreadPriority(12);
// Changes the Priority of passed Thread (first param)
Process.setThreadPriority(Process.myTid(), 12);

  这里 range 的范围是 -20 (高) 到 +19 (低). 不要选得 太高 

  最好使用预先定义在 android.os.Process 的constants :
Java: 
          // Lower is 'more impotant'
Process.THREAD_PRIORITY_LOWEST = 19
Process.THREAD_PRIORITY_BACKGROUND = 5
Process.THREAD_PRIORITY_DEFAULT = 0
Process.THREAD_PRIORITY_FOREGROUND = -5
Process.THREAD_PRIORITY_DISPLAY = -10
Process.THREAD_PRIORITY_URGENT_DISPLAY = -15

原创粉丝点击