ThreadPriority 열거형

정의

Thread의 예약 우선 순위를 지정합니다.

public enum class ThreadPriority
public enum ThreadPriority
[System.Serializable]
public enum ThreadPriority
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum ThreadPriority
type ThreadPriority = 
[<System.Serializable>]
type ThreadPriority = 
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ThreadPriority = 
Public Enum ThreadPriority
상속
ThreadPriority
특성

필드

AboveNormal 3

Thread는 우선 순위가 Highest인 스레드 뒤와 우선 순위가 Normal인 스레드 앞에 예약할 수 있습니다.

BelowNormal 1

Thread는 우선 순위가 Normal인 스레드 뒤와 우선 순위가 Lowest인 스레드 앞에 예약할 수 있습니다.

Highest 4

Thread는 다른 우선 순위가 할당된 스레드 앞에 예약할 수 있습니다.

Lowest 0

Thread는 다른 우선 순위가 할당된 스레드 뒤에 예약할 수 있습니다.

Normal 2

Thread는 우선 순위가 AboveNormal인 스레드 뒤와 우선 순위가 BelowNormal인 스레드 앞에 예약할 수 있습니다. 스레드에는 기본적으로 Normal 우선 순위가 할당됩니다.

예제

다음 코드 예제에서는 스레드의 우선 순위를 변경한 결과를 보여줍니다. 세 개의 스레드가 생성되고, 한 스레드의 우선 순위가 BelowNormal로 설정되고, 두 번째 스레드의 우선 순위가 AboveNormal로 설정됩니다. 각 스레드는 루프에서 변수를 while 증가시키고 설정된 시간 동안 실행됩니다.

using System;
using System.Threading;
using Timers = System.Timers;

class Test
{
    static void Main()
    {
        PriorityTest priorityTest = new PriorityTest();

        Thread thread1 = new Thread(priorityTest.ThreadMethod);
        thread1.Name = "ThreadOne";
        Thread thread2 = new Thread(priorityTest.ThreadMethod);
        thread2.Name = "ThreadTwo";
        thread2.Priority = ThreadPriority.BelowNormal;
        Thread thread3 = new Thread(priorityTest.ThreadMethod);
        thread3.Name = "ThreadThree";
        thread3.Priority = ThreadPriority.AboveNormal;

        thread1.Start();
        thread2.Start();
        thread3.Start();
        // Allow counting for 10 seconds.
        Thread.Sleep(10000);
        priorityTest.LoopSwitch = false;
    }
}

class PriorityTest
{
    static volatile bool loopSwitch;
    [ThreadStatic] static long threadCount = 0;

    public PriorityTest()
    {
        loopSwitch = true;
    }

    public bool LoopSwitch
    {
        set{ loopSwitch = value; }
    }

    public void ThreadMethod()
    {
        while(loopSwitch)
        {
            threadCount++;
        }
        Console.WriteLine("{0,-11} with {1,11} priority " +
            "has a count = {2,13}", Thread.CurrentThread.Name, 
            Thread.CurrentThread.Priority.ToString(), 
            threadCount.ToString("N0")); 
    }
}
// The example displays output like the following:
//    ThreadOne   with      Normal priority has a count =   755,897,581
//    ThreadThree with AboveNormal priority has a count =   778,099,094
//    ThreadTwo   with BelowNormal priority has a count =     7,840,984
Imports System.Threading
Imports Timers = System.Timers

Public Module Example
   Dim t As Timers.Timer
   Private priorityTest As New PriorityTest()

    Public Sub Main()
        Dim thread1 As New Thread(AddressOf priorityTest.ThreadMethod)
        thread1.Name = "ThreadOne"
        Dim thread2 As New Thread(AddressOf priorityTest.ThreadMethod)
        thread2.Name = "ThreadTwo"
        thread2.Priority = ThreadPriority.BelowNormal
        Dim thread3 As New Thread(AddressOf priorityTest.ThreadMethod)
        thread3.Name = "ThreadThree"
        thread3.Priority = ThreadPriority.AboveNormal
        thread1.Start()
        thread2.Start()
        thread3.Start()

        ' Allow threads to execute for about 10 seconds.
        t = New Timers.Timer()
        t.AutoReset = False
        t.Interval = 10000
        AddHandler t.Elapsed, AddressOf Elapsed
        t.Start()
    End Sub

    Private Sub Elapsed(sender As Object, e As Timers.ElapsedEventArgs)
       priorityTest.LoopSwitch = False
    End Sub
End Module

Public Class PriorityTest
    Private Shared loopSwitchValue As Boolean
    <ThreadStatic> Shared threadCount As Long

    Sub New()
        loopSwitchValue = True
    End Sub

    WriteOnly Property LoopSwitch As Boolean
        Set
            loopSwitchValue = Value
        End Set
    End Property

    Sub ThreadMethod()
        Do While True
            threadCount += 1
            If Not loopSwitchValue Then Exit Do
        Loop

        Console.WriteLine("{0,-11} with {1,11} priority " &
            "has a count = {2,13}", Thread.CurrentThread.Name,
            Thread.CurrentThread.Priority.ToString(),
            threadCount.ToString("N0")) 
    End Sub
End Class
' The example displays the following output:
'    ThreadOne   with      Normal priority has a count =   755,897,581
'    ThreadThree with AboveNormal priority has a count =   778,099,094
'    ThreadTwo   with BelowNormal priority has a count =     7,840,984

설명

ThreadPriority 는 스레드 우선 순위에 대해 가능한 모든 값 집합을 정의합니다. 스레드 우선 순위는 한 스레드와 다른 스레드의 상대적 우선 순위를 지정합니다.

모든 스레드에는 할당된 우선 순위가 있습니다. 런타임 내에서 만든 스레드는 처음에 우선 순위가 Normal 할당되지만 런타임 외부에서 만든 스레드는 런타임에 들어갈 때 이전 우선 순위를 유지합니다. 해당 Priority 속성에 액세스하여 스레드의 우선 순위를 가져와서 설정할 수 있습니다.

스레드는 우선 순위에 따라 실행되도록 예약됩니다. 스레드 실행 순서를 결정하는 데 사용되는 예약 알고리즘은 운영 체제마다 다릅니다. 또한 운영 체제는 사용자 인터페이스의 포커스가 포그라운드와 백그라운드 간에 이동되므로 스레드 우선 순위를 동적으로 조정할 수 있습니다.

스레드의 우선 순위는 스레드의 상태에 영향을 주지 않습니다. 스레드의 상태는 운영 체제에서 예약할 Running 수 있기 전에 있어야 합니다.

적용 대상

추가 정보