📜  C#|多线程中的线程优先级

📅  最后修改于: 2021-05-29 13:46:23             🧑  作者: Mango

在多线程环境中,每个线程都有自己的优先级。线程的优先级显示了线程获得CPU资源访问的频率。每当我们在C#中创建线程时,始终会为其分配一些优先级。

重要事项:

  • 程序员可以为线程明确分配优先级。
  • 默认情况下,线程的优先级为Normal
  • 操作系统未分配线程优先级。
  • 如果线程已达到最终状态(例如Aborted) ,则将给出ThreadStateException
  • 如果为set操作指定的值不是有效的ThreadPriority值,则将提供ArgumentException
  • 不能保证优先级高的线程将首先执行,优先级低的线程将在之后执行。由于上下文切换,最高优先级线程可以在最低优先级线程之后执行。
  • 在为线程分配优先级时,需要小心,如果不谨慎,则可能会出现线程饥饿
  • 线程优先级将始终取决于进程优先级或父容器,因此将线程优先级设置为最高不会对应于实时执行。

如何设置并获取线程的优先级?

Thread.Priority属性用于获取或设置一个值,该值指示线程的调度优先级。

句法:

public ThreadPriority Priority{ get; set; }

在这里, System.Threading名称空间下的ThreadPriority枚举负责指定线程的调度优先级,并且这些优先级是:

  • 最高:此优先级的值为4。
  • AboveNormal:该优先级的值为3。
  • 正常:该优先级的值为2。
  • 低于正常:此优先级的值为1。
  • 最低:此优先级的值为0。

属性值: ThreadPriority值之一。默认值为“正常”。

例外情况:

  • ThreadStateException:如果线程已达到最终状态,如异常终止。
  • ArgumentException:如果为set操作指定的值不是有效的ThreadPriority值。

范例1:

// C# program to illustrate how to
// set and get the priority of threads
using System;
using System.Threading;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Creating and initializing threads
        Thread T1 = new Thread(work);
        Thread T2 = new Thread(work);
        Thread T3 = new Thread(work);
  
        // Set the priority of threads
        T2.Priority = ThreadPriority.Highest;
        T3.Priority = ThreadPriority.BelowNormal;
        T1.Start();
        T2.Start();
        T3.Start();
  
        // Display the priority of threads
        Console.WriteLine("The priority of T1 is: {0}",
                                          T1.Priority);
  
        Console.WriteLine("The priority of T2 is: {0}",
                                          T2.Priority);
  
        Console.WriteLine("The priority of T3 is: {0}",
                                          T3.Priority);
    }
  
    public static void work()
    {
  
        // Sleep for 1 second
        Thread.Sleep(1000);
    }
}

输出:

The priority of T1 is: Normal
The priority of T2 is: Highest
The priority of T3 is: BelowNormal

范例2:

// C# program to illustrate the
// Priority property of Thread class
using System;
using System.Threading;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Creating and initializing threads
        Thread T1 = new Thread(work1);
        Thread T2 = new Thread(work2);
  
        // Set the priority of threads
        // Here T2 thread executes first 
        // because the Priority of T2 is
        // highest as compare to T1 thread
        T1.Priority = ThreadPriority.Lowest;
        T2.Priority = ThreadPriority.Highest;
        T1.Start();
        T2.Start();
    }
    public static void work1()
    {
  
        Console.WriteLine("T1 thread is working..");
    }
    public static void work2()
    {
  
        Console.WriteLine("T2 thread is working..");
    }
}

输出:

T2 thread is working..
T1 thread is working..

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.thread.priority?view=netframework-4.7.2