Changing thread priority doesn't have an effect

后端 未结 1 1105
粉色の甜心
粉色の甜心 2020-12-30 07:00

I\'m trying to change priority of main thread using android.os.Process.setThreadPriority(). I have log messages before and after priority changing, here is code

相关标签:
1条回答
  • 2020-12-30 07:36

    You are using the wrong ThreadID for your check.

    To get the correct id you have to use android.os.Process.myTid();

    Fixed code:

    package mypackage.test;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    
    public class TestActivity extends Activity {
        public final String TAG="TestActivity";
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            int tid=android.os.Process.myTid();
    
            Log.d(TAG,"priority before change = " + android.os.Process.getThreadPriority(tid));
            Log.d(TAG,"priority before change = "+Thread.currentThread().getPriority());
            android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_DISPLAY);
            Log.d(TAG,"priority after change = " + android.os.Process.getThreadPriority(tid));
            Log.d(TAG,"priority after change = " + Thread.currentThread().getPriority());
        }
    }
    

    Log output:

    priority before change = 0
    priority before change = 5
    priority after change = -4
    priority after change = 5
    

    For further reference:

    http://developer.android.com/reference/android/os/Process.html#myTid()

    0 讨论(0)
提交回复
热议问题