> Java > java지도 시간 > Java에서 스레드 우선순위의 중요성은 무엇입니까?

Java에서 스레드 우선순위의 중요성은 무엇입니까?

王林
풀어 주다: 2023-08-27 08:25:06
앞으로
967명이 탐색했습니다.

Java에서 스레드 우선순위의 중요성은 무엇입니까?

멀티 스레드 애플리케이션에서는 각 스레드에 우선 순위가 할당됩니다. 프로세서는 우선순위에 따라 스레드 스케줄러에 의해 스레드에 할당됩니다(예: 우선순위가 가장 높은 스레드에 프로세서가 먼저 할당되는 식). 스레드의 기본 우선순위 5입니다. Thread 클래스의 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.<strong>getPriority</strong>());
      System.out.println("Default thread priority of thread2: " + thread2.<strong>getPriority</strong>());
      System.out.println("Default thread priority of thread3: " + thread3.<strong>getPriority</strong>());
      thread1.setPriority(8);
      thread2.setPriority(3);
      thread3.setPriority(6);
      System.out.println("New thread priority of thread1: " + thread1.<strong>getPriority()</strong>);
      System.out.println("New thread priority of thread2: " + thread2.<strong>getPriority()</strong>);
      System.out.println("New thread priority of thread3: " + thread3.<strong>getPriority()</strong>);
   }
}
로그인 후 복사

출력

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
로그인 후 복사

위 내용은 Java에서 스레드 우선순위의 중요성은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿