C#에서 스레드(thread)의 우선순위를 확인하려면 Priority 속성을 사용하면 됩니다. 이 속성은 해당 스레드가 운영체제 스케줄러로부터 얼마나 높은 실행 순위를 받는지를 나타내며, 멀티스레딩 환경에서 작업 처리 순서를 제어할 때 중요하게 활용됩니다.
1. CurrentThread 속성으로 현재 스레드 가져오기
먼저 CurrentThread 속성을 사용하여 현재 실행 중인 스레드에 대한 정보를 가져옵니다.
Thread thread = Thread.CurrentThread;2. Priority 속성으로 우선순위 표시하기
가져온 스레드 객체의 thread.Priority 속성을 참조하면 해당 스레드의 우선순위를 간단히 출력할 수 있습니다.
thread.Priority전체 예제 코드
다음은 C#에서 스레드의 우선순위를 표시하는 완전한 코드입니다.
using System;
using System.Threading;
namespace Demo {
class MyClass {
static void Main(string[] args) {
Thread thread = Thread.CurrentThread;
thread.Name = "My Thread";
Console.WriteLine("Thread Priority = {0}", thread.Priority);
Console.ReadKey();
}
}
}실행 결과
Thread Priority = Normal실행 결과를 보면 별도로 설정하지 않은 스레드의 기본 우선순위는 Normal로 지정되어 있음을 확인할 수 있습니다. 참고로 C#의 ThreadPriority 열거형에는 Lowest, BelowNormal, Normal, AboveNormal, Highest 다섯 가지 수준이 있으며, 필요에 따라 thread.Priority = ThreadPriority.Highest;처럼 직접 우선순위를 변경할 수도 있습니다.