Changing thread priority doesn't have an effec

2019-02-04 14:39发布

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:

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=(int)Thread.currentThread().getId();
        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());
    }
}

And I get the following output:

priority before change (getThreadPriority) = 0
priority before change (currentThread().getPriority) = 5
priority after change (getThreadPriority) = 0
priority after change (currentThread().getPriority) = 5

It means that priority didn't change, whatever method I use to evaluate it. android.os.Process.THREAD_PRIORITY_DISPLAY = -4, so after changing my ouput should equal -4, why it remains the same? Why android.os.Process.setThreadPriority() has no effect?

P.S. I read google docs about threads, but I didn't come across any issues explaining difference between android.os.Process.getThreadPriority() and Thread.currentThread().getPriority(). Why these methods return different values?

P.P.S. Thread.currentThread().setPriority() works fine.

1条回答
Animai°情兽
2楼-- · 2019-02-04 15:29

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()

查看更多
登录 后发表回答