スレッド優先順位設定方法Android



Thread Priority Setting Method Android



時々、時間のかかるタスクを操作するスレッドを作成しますが、それは高い優先度を必要とせず、時間のかかる作業をこのスレッドに適用できず、全体的なパフォーマンスに影響を与えます。その後、Process.setThreadPriority(int i)この関数は、スレッドの優先順位を設定します。値が大きいほど、優先度は低くなります。
SDKは、次のように多くの優先度レベルの値で定義されています。

THREAD_PRIORITY_DEFAULT, default thread priority is 0. THREAD_PRIORITY_LOWEST, the lowest level of the thread, is 19. THREAD_PRIORITY_BACKGROUND background thread recommend setting this priority is 10. THREAD_PRIORITY_FOREGROUND user is interacting with the UI thread, the code can not set the priority will be adjusted according to the priority of the case, it is -2. THREAD_PRIORITY_DISPLAY is related to the interaction of the priority sectors and UI, but priority than THREAD_PRIORITY_FOREGROUND, the code can not be set, adjusted by the system in accordance with, the value is -4. The highest level THREAD_PRIORITY_URGENT_DISPLAY display thread for handling input events drawing picture and retrieval, the code can not be set as the priority. Value -8. Standard sound level THREAD_PRIORITY_AUDIO thread, the code can not be set for this priority, value-16. The sound highest level THREAD_PRIORITY_URGENT_AUDIO thread priority degree higher than THREAD_PRIORITY_AUDIO. The code can not be set for this priority. Value of -19. THREAD_PRIORITY_MORE_FAVORABLE THREAD_PRIORITY_DEFAULT slightly relative priority value of -1. THREAD_PRIORITY_LESS_FAVORABLE THREAD_PRIORITY_DEFAULT relatively little behind some value 1.

たとえば、スキャンタスクのスキャナーは、サブスレッドを開き、その優先度を設定し、一部の平均バックグラウンドスレッド優先度よりもさらに低く表現します



Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_LESS_FAVORABLE)