ThreadPriority 열거형
정의
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
- 상속
- 특성
필드
AboveNormal | 3 | Thread는 우선 순위가 |
BelowNormal | 1 | Thread는 우선 순위가 |
Highest | 4 | Thread는 다른 우선 순위가 할당된 스레드 앞에 예약할 수 있습니다.The Thread can be scheduled before threads with any other priority. |
Lowest | 0 | Thread는 다른 우선 순위가 할당된 스레드 뒤에 예약할 수 있습니다.The Thread can be scheduled after threads with any other priority. |
Normal | 2 | Thread는 우선 순위가 |
예제
다음 코드 예제에서는 스레드의 우선 순위를 변경한 결과를 보여 줍니다.The following code example shows the result of changing the priority of a thread. 세 개의 스레드가 만들어지고, 한 스레드의 우선 순위는 BelowNormal로 설정 되 고, 두 번째 스레드의 우선 순위는 AboveNormal로 설정 됩니다.Three threads are created, the priority of one thread is set to BelowNormal, and the priority of a second is set to AboveNormal. 각 스레드는 루프에서 변수를 증가 시키고 while
설정 된 시간 동안 실행 됩니다.Each thread increments a variable in a while
loop and runs for a set time.
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 스레드 우선 순위에 대 한 모든 가능한 값 집합을 정의 합니다.ThreadPriority defines the set of all possible values for a thread priority. 스레드 우선 순위는 한 스레드와 다른 스레드의 상대적 우선 순위를 지정 합니다.Thread priorities specify the relative priority of one thread versus another.
모든 스레드에는 할당 된 우선 순위가 있습니다.Every thread has an assigned priority. 런타임에 생성 된 스레드는 Normal
우선 순위를 처음 할당 하는 반면, 런타임 외부에서 만든 스레드는 런타임에 시작 될 때 이전 우선 순위를 유지 합니다.Threads created within the runtime are initially assigned the Normal
priority, while threads created outside the runtime retain their previous priority when they enter the runtime. 속성에 액세스 하 여 스레드의 우선 순위를 가져오고 설정할 수 있습니다 Priority .You can get and set the priority of a thread by accessing its Priority property.
스레드는 우선 순위에 따라 실행되도록 예약됩니다.Threads are scheduled for execution based on their priority. 스레드 실행 순서를 결정 하는 데 사용 되는 일정 알고리즘은 각 운영 체제에 따라 다릅니다.The scheduling algorithm used to determine the order of thread execution varies with each operating system. 운영 체제는 사용자 인터페이스의 포커스가 포그라운드 및 백그라운드 사이에서 이동할 때 스레드 우선 순위를 동적으로 조정할 수도 있습니다.The operating system can also adjust the thread priority dynamically as the user interface's focus is moved between the foreground and the background.
스레드의 우선 순위는 스레드의 상태에 영향을 주지 않습니다. 스레드의 상태는 Running 운영 체제에서 일정을 예약 하기 전에 있어야 합니다.The priority of a thread does not affect the thread's state; the state of the thread must be Running before the operating system can schedule it.