ThreadPriority ThreadPriority ThreadPriority ThreadPriority Enum

定义

指定 Thread 的调度优先级。Specifies the scheduling priority of a Thread.

public enum class ThreadPriority
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum ThreadPriority
type ThreadPriority = 
Public Enum ThreadPriority
继承
ThreadPriorityThreadPriorityThreadPriorityThreadPriority
属性

字段

AboveNormal AboveNormal AboveNormal AboveNormal 3

可以将 Thread 安排在具有 Highest 优先级的线程之后,在具有 Normal 优先级的线程之前。The Thread can be scheduled after threads with Highest priority and before those with Normal priority.

BelowNormal BelowNormal BelowNormal BelowNormal 1

可以将 Thread 安排在具有 Normal 优先级的线程之后,在具有 Lowest 优先级的线程之前。The Thread can be scheduled after threads with Normal priority and before those with Lowest priority.

Highest Highest Highest Highest 4

可以将 Thread 安排在具有任何其他优先级的线程之前。The Thread can be scheduled before threads with any other priority.

Lowest Lowest Lowest Lowest 0

可以将 Thread 安排在具有任何其他优先级的线程之后。The Thread can be scheduled after threads with any other priority.

Normal Normal Normal Normal 2

可以将 Thread 安排在具有 AboveNormal 优先级的线程之后,在具有 BelowNormal 优先级的线程之前。The Thread can be scheduled after threads with AboveNormal priority and before those with BelowNormal priority. 默认情况下,线程具有 Normal 优先级。Threads have Normal priority by default.

示例

下面的代码示例演示更改线程优先级的结果。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 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.

适用于

另请参阅