Changing thread priority doesn't have an effect

我与影子孤独终老i 提交于 2019-11-29 07:07:38

问题


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:


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



来源:https://stackoverflow.com/questions/9019137/changing-thread-priority-doesnt-have-an-effect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!