안 드 로 이 드 설정 스 레 드 우선 순위 두 가지 방식
2651 단어 android스 레 드우선 순위setPriority
다음으로 이동: http://www.eoeandroid.com/thread-112252-1-1.html
1)android.os.Process.setThreadPriority(int priority)또는 android.os.Process.setThreadPriority(int tid,int priority)priority:[-20,19],높 은 우선 순위->낮은 우선 순위.(2)java.lang.Thread.setPriority(int priority)priority:[1,10],낮은 우선 순위->높 은 우선 순위.테스트 결과,안 드 로 이 드 자체 API(첫 번 째 방법)설정 의 우선 순 위 를 사용 하면 스 레 드 스케줄 에 현저 한 영향 을 줍 니 다.
------------------------------------------------
import android.app.Activity;
import android.os.Bundle;
import android.os.Process;
import android.util.Log;
public class TestThreadPriority extends Activity {
private static final String TAG = "TestThreadPriority";
private boolean mNeedExit = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyThread a = new MyThread("Thread A");
a.setOSPriority(Process.THREAD_PRIORITY_LOWEST); // 19
a.setPriority(Thread.MAX_PRIORITY); // 10
MyThread b = new MyThread("Thread B");
b.setOSPriority(Process.THREAD_PRIORITY_URGENT_AUDIO); // -19
b.setPriority(Thread.MIN_PRIORITY); // 1
a.start();
b.start();
}
@Override
public void onBackPressed() {
mNeedExit = true;
super.onBackPressed();
}
private class MyThread extends Thread {
private int mOSPriority = Process.THREAD_PRIORITY_DEFAULT;
private int mLoopCount = 0;
public MyThread(String threadName) {
super(threadName);
}
public void setOSPriority(int p) {
mOSPriority = p;
}
@Override
public void run() {
Process.setThreadPriority(mOSPriority);
while (!mNeedExit) {
mLoopCount++;
Math.log(Math.random() * 1000); // calculation test
Log.d(TAG,
new StringBuilder().append(getName())
.append(" os priority: ").append(mOSPriority)
.append(" java priority: ")
.append(getPriority()).append(" loop count: ")
.append(mLoopCount).toString());
}
Log.d(TAG,
new StringBuilder().append(getName()).append(" exiting...")
.append(" os priority: ").append(mOSPriority)
.append(" java priority: ").append(getPriority())
.append(" loop count: ").append(mLoopCount)
.toString());
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.