> Java > java지도 시간 > 본문

멀티스레딩의 Java 스레드 우선순위

王林
풀어 주다: 2023-09-06 14:21:06
앞으로
781명이 탐색했습니다.

멀티스레딩의 Java 스레드 우선순위

멀티스레딩의 경우 스레드 스케줄러는 다양한 조건에 따라 스레드를 특정 프로세스에 할당합니다. 그들의 우선순위. Java 스레드에는 사전 할당된 우선순위가 있습니다. 또한, 자바 가상 기계는 스레드에 우선순위를 할당하거나 프로그래머가 명시적으로 지정할 수도 있습니다. 범위는 스레드 우선순위의 값은 1에서 10(포함) 사이입니다. 세 가지 정적 변수 우선순위와 관련된 것은 -

  • MAX_PRIORITY - 스레드의 최대 우선순위이며 기본값은 10입니다.

  • NORM_PRIORITY - 스레드의 기본 우선순위이며 기본값은 5입니다.

  • MIN_PRIORITY - 스레드의 최소 우선순위이며 기본값은 1입니다.

Java의 "getPriority()" 메서드는 바인딩된 스레드 우선순위를 값으로 반환하는 데 도움이 됩니다.

"setPriority()" 메소드는 주어진 스레드의 우선순위 값을 변경합니다. 그것은 던진다 IllegalArgumentException은 스레드 우선순위가 1보다 작거나 10보다 클 때 발생합니다.

Example

실시간 데모

import java.lang.*;
public class Demo extends Thread{
   public void run(){
      System.out.println("Now, inside the run method");
   }
   public static void main(String[]args){
      Demo my_thr_1 = new Demo();
      Demo my_thr_2 = new Demo();
      System.out.println("The thread priority of first thread is : " + my_thr_1.getPriority());
      System.out.println("The thread priority of first thread is : " +       my_thr_2.getPriority());
      my_thr_1.setPriority(5);
      my_thr_2.setPriority(3);
      System.out.println("The thread priority of first thread is : " +    my_thr_1.getPriority());
      System.out.println("The thread priority of first thread is : " + my_thr_2.getPriority());
      System.out.print(Thread.currentThread().getName());
      System.out.println("The thread priority of main thread is : " +
      Thread.currentThread().getPriority());
      Thread.currentThread().setPriority(10);
      System.out.println("The thread priority of main thread is : " +
      Thread.currentThread().getPriority());
   }
}
로그인 후 복사

Output

The thread priority of first thread is : 5
The thread priority of first thread is : 5
The thread priority of first thread is : 5
The thread priority of first thread is : 3
The thread priority of main thread is : 5
The thread priority of main thread is : 10
로그인 후 복사

Demo라는 클래스는 기본 클래스 Thread에서 상속됩니다. 'run' 함수가 정의되어 있고 관련성이 있습니다. 메시지가 정의됩니다. 기본 기능에서는 Demo 클래스의 두 인스턴스가 생성되고 우선순위는 "getPriority" 함수를 호출하여 찾습니다.

콘솔에 인쇄되어 있습니다. 다음으로 다음을 사용하여 데모 인스턴스에 우선순위를 할당합니다. '우선순위 설정' 기능. 출력이 콘솔에 표시됩니다. 스레드 이름을 인쇄하세요. "getName" 함수를 사용하여 화면에 표시됩니다.

위 내용은 멀티스레딩의 Java 스레드 우선순위의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:tutorialspoint.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!