다중 스레드 응용 프로그램에서 각 스레드에는 우선 순위가 할당됩니다. 프로세서는 스레드 스케줄러에 의해 스레드에 할당됩니다. 우선 순위에 따라 즉, 가장 높은 우선 순위 스레드가 프로세서에 먼저 할당되는 식입니다. 기본 우선순위 값이 '5'인 스레드 . getPriority() 를 사용하여 스레드의 우선순위를 얻을 수 있습니다. Thread 클래스의 메소드
세 가지 정적 값 스레드에 정의됨 스레드의 우선 순위에 대한 클래스
MAX_PRIORITY
값이 10인 최대 스레드 우선순위입니다. .
NORM_PRIORITY
기본값입니다. 값이 5인 스레드 우선순위 .
MIN_PRIORITY
1 값을 갖는 최소 스레드 우선순위입니다. .
구문
public final int getPriority()
예시
public class ThreadPriorityTest extends Thread {
public static void main(String[]args) {
ThreadPriorityTest thread1 = new ThreadPriorityTest();
ThreadPriorityTest thread2 = new ThreadPriorityTest();
ThreadPriorityTest thread3 = new ThreadPriorityTest();
System.out.println("Default thread priority of thread1: " + thread1.getPriority());
System.out.println("Default thread priority of thread2: " + thread2.getPriority());
System.out.println("Default thread priority of thread3: " + thread3.getPriority());
thread1.setPriority(8);
thread2.setPriority(3);
thread3.setPriority(6);
System.out.println("New thread priority of thread1: " + thread1.getPriority());
System.out.println("New thread priority of thread2: " + thread2.getPriority());
System.out.println("New thread priority of thread3: " + thread3.getPriority());
}
} 출력
Default thread priority of thread1: 5 Default thread priority of thread2: 5 Default thread priority of thread3: 5 New thread priority of thread1: 8 New thread priority of thread2: 3 New thread priority of thread3: 6