Android - Thread Priority
Aus Wikizone
Version vom 4. März 2013, 20:36 Uhr von 134.3.241.116 (Diskussion) (Die Seite wurde neu angelegt: „Beispiel: <pre> package mypackage.test; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class TestActivity extends Activit…“)
Beispiel:
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());
}
}
priority before change = 0 priority before change = 5 priority after change = -4 priority after change = 5
2 Möglichkeiten: http://stackoverflow.com/questions/5198518/whats-the-difference-between-thread-setpriority-and-android-os-process-setthre
Runnable r = ...; Thread thread = new Thread(r); thread.setPriority((Thread.MAX_PRIORITY + Thread.NORM_PRIORITY) / 2);
Runnable r = ...
Thread thread = new Thread( new Runnable() {
public void run() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_MORE_FAVORABLE);
r.run();
}
});
Kommentare:
The current Dalvik implementation seems to map Java Threads ony by one to the underlying linux system PTHREADs like you say. All Threads of all apps belong on the same thread group on the system, so every Thread competes with all Threads of all apps.
So currently Thread.setPriority should actually do the same as Process.setThreadPritority, using the smaller Java priority scale, the mapping of prioritys is defined in kNiceValues at vm/Thread.c
However, using Thread.setPriority() you are using a scale of 0-10 and on Process.setThreadPriority() you are using the nice scale (+/- 20).