线程优先级
Priority | Summary |
Lowest = 0 | The System.Threading.Thread can be scheduled after threads with any other priority |
BelowNormal = 1 | The System.Threading.Thread can be scheduled after threads with Normal priority and before those with Lowest priority. |
Normal = 2 | The System.Threading.Thread can be scheduled after threads with AboveNormal priority and before those with BelowNormal priority. Threads have Normal priority by default. |
AboveNormal = 3 | The System.Threading.Thread can be scheduled after threads with Highest priority and before those with Normal priority. |
Highest = 4 | The System.Threading.Thread can be scheduled before threads with any other priority. |
当线程的优先级越高,获得运行的机会就会越大,但这并不代表优先级低的没有运行的机会。对于相同优先级的线程,系统调度器会根据一定算法随机选中某一个运行。
private object lockObj = new object(); public void Test() { for (int i = 0; i < 50; i++) { Thread thread = new Thread(new ThreadStart(Execute)); thread.Name = "Thread-" + i; int p = i / 10; thread.Priority = (ThreadPriority)p; thread.Start(); } Thread.Sleep(60*60*1000); } private void Execute() { Monitor.Enter(lockObj); for (int i = 0; i < 3; i++) { Console.WriteLine("Name:" + Thread.CurrentThread.Name + ", Priority:"+Thread.CurrentThread.Priority +" , Count:" + i); Thread.Sleep(new Random().Next(1000)); } Monitor.Exit(lockObj); }
控制台输出:
Name:Thread-0, Priority:Lowest , Count:0
Name:Thread-0, Priority:Lowest , Count:1
Name:Thread-0, Priority:Lowest , Count:2
Name:Thread-45, Priority:Highest , Count:0
Name:Thread-45, Priority:Highest , Count:1
Name:Thread-45, Priority:Highest , Count:2
Name:Thread-49, Priority:Highest , Count:0
Name:Thread-49, Priority:Highest , Count:1
Name:Thread-49, Priority:Highest , Count:2
Name:Thread-18, Priority:BelowNormal , Count:0
Name:Thread-18, Priority:BelowNormal , Count:1
Name:Thread-18, Priority:BelowNormal , Count:2
Name:Thread-42, Priority:Highest , Count:0
Name:Thread-42, Priority:Highest , Count:1
Name:Thread-42, Priority:Highest , Count:2
Name:Thread-48, Priority:Highest , Count:0
Name:Thread-48, Priority:Highest , Count:1
Name:Thread-48, Priority:Highest , Count:2
Name:Thread-47, Priority:Highest , Count:0
Name:Thread-47, Priority:Highest , Count:1
Name:Thread-47, Priority:Highest , Count:2
Name:Thread-46, Priority:Highest , Count:0
Name:Thread-46, Priority:Highest , Count:1
Name:Thread-46, Priority:Highest , Count:2
Name:Thread-44, Priority:Highest , Count:0
Name:Thread-44, Priority:Highest , Count:1
Name:Thread-44, Priority:Highest , Count:2
Name:Thread-41, Priority:Highest , Count:0
Name:Thread-41, Priority:Highest , Count:1
Name:Thread-41, Priority:Highest , Count:2
Name:Thread-14, Priority:BelowNormal , Count:0
Name:Thread-14, Priority:BelowNormal , Count:1
Name:Thread-14, Priority:BelowNormal , Count:2
Name:Thread-43, Priority:Highest , Count:0
Name:Thread-43, Priority:Highest , Count:1
Name:Thread-43, Priority:Highest , Count:2
Name:Thread-39, Priority:AboveNormal , Count:0
Name:Thread-39, Priority:AboveNormal , Count:1
Name:Thread-39, Priority:AboveNormal , Count:2
Name:Thread-40, Priority:Highest , Count:0
Name:Thread-40, Priority:Highest , Count:1
Name:Thread-40, Priority:Highest , Count:2
Name:Thread-37, Priority:AboveNormal , Count:0
Name:Thread-37, Priority:AboveNormal , Count:1
Name:Thread-37, Priority:AboveNormal , Count:2
Name:Thread-38, Priority:AboveNormal , Count:0
Name:Thread-38, Priority:AboveNormal , Count:1
Name:Thread-38, Priority:AboveNormal , Count:2
Name:Thread-19, Priority:BelowNormal , Count:0
Name:Thread-19, Priority:BelowNormal , Count:1
Name:Thread-19, Priority:BelowNormal , Count:2
Name:Thread-36, Priority:AboveNormal , Count:0
Name:Thread-36, Priority:AboveNormal , Count:1
Name:Thread-36, Priority:AboveNormal , Count:2
Name:Thread-33, Priority:AboveNormal , Count:0
Name:Thread-33, Priority:AboveNormal , Count:1
Name:Thread-33, Priority:AboveNormal , Count:2
Name:Thread-15, Priority:BelowNormal , Count:0
Name:Thread-15, Priority:BelowNormal , Count:1
Name:Thread-15, Priority:BelowNormal , Count:2
Name:Thread-32, Priority:AboveNormal , Count:0
Name:Thread-32, Priority:AboveNormal , Count:1
Name:Thread-32, Priority:AboveNormal , Count:2
Name:Thread-35, Priority:AboveNormal , Count:0
Name:Thread-35, Priority:AboveNormal , Count:1
Name:Thread-35, Priority:AboveNormal , Count:2
Name:Thread-29, Priority:Normal , Count:0
Name:Thread-29, Priority:Normal , Count:1
Name:Thread-29, Priority:Normal , Count:2
Name:Thread-34, Priority:AboveNormal , Count:0
Name:Thread-34, Priority:AboveNormal , Count:1
Name:Thread-34, Priority:AboveNormal , Count:2
Name:Thread-31, Priority:AboveNormal , Count:0
Name:Thread-31, Priority:AboveNormal , Count:1
Name:Thread-31, Priority:AboveNormal , Count:2
Name:Thread-30, Priority:AboveNormal , Count:0
Name:Thread-30, Priority:AboveNormal , Count:1
Name:Thread-30, Priority:AboveNormal , Count:2
Name:Thread-27, Priority:Normal , Count:0
Name:Thread-27, Priority:Normal , Count:1
Name:Thread-27, Priority:Normal , Count:2
Name:Thread-26, Priority:Normal , Count:0
Name:Thread-26, Priority:Normal , Count:1
Name:Thread-26, Priority:Normal , Count:2
Name:Thread-23, Priority:Normal , Count:0
Name:Thread-23, Priority:Normal , Count:1
Name:Thread-23, Priority:Normal , Count:2
Name:Thread-22, Priority:Normal , Count:0
Name:Thread-22, Priority:Normal , Count:1
Name:Thread-22, Priority:Normal , Count:2
Name:Thread-11, Priority:BelowNormal , Count:0
Name:Thread-11, Priority:BelowNormal , Count:1
Name:Thread-11, Priority:BelowNormal , Count:2
Name:Thread-10, Priority:BelowNormal , Count:0
Name:Thread-10, Priority:BelowNormal , Count:1
Name:Thread-10, Priority:BelowNormal , Count:2
Name:Thread-7, Priority:Lowest , Count:0
Name:Thread-7, Priority:Lowest , Count:1
Name:Thread-7, Priority:Lowest , Count:2
Name:Thread-6, Priority:Lowest , Count:0
Name:Thread-6, Priority:Lowest , Count:1
Name:Thread-6, Priority:Lowest , Count:2
Name:Thread-3, Priority:Lowest , Count:0
Name:Thread-3, Priority:Lowest , Count:1
Name:Thread-3, Priority:Lowest , Count:2
Name:Thread-2, Priority:Lowest , Count:0
Name:Thread-2, Priority:Lowest , Count:1
Name:Thread-2, Priority:Lowest , Count:2
Name:Thread-28, Priority:Normal , Count:0
Name:Thread-28, Priority:Normal , Count:1
Name:Thread-28, Priority:Normal , Count:2
Name:Thread-25, Priority:Normal , Count:0
Name:Thread-25, Priority:Normal , Count:1
Name:Thread-25, Priority:Normal , Count:2
Name:Thread-24, Priority:Normal , Count:0
Name:Thread-24, Priority:Normal , Count:1
Name:Thread-24, Priority:Normal , Count:2
Name:Thread-21, Priority:Normal , Count:0
Name:Thread-21, Priority:Normal , Count:1
Name:Thread-21, Priority:Normal , Count:2
Name:Thread-20, Priority:Normal , Count:0
Name:Thread-20, Priority:Normal , Count:1
Name:Thread-20, Priority:Normal , Count:2
Name:Thread-12, Priority:BelowNormal , Count:0
Name:Thread-12, Priority:BelowNormal , Count:1
Name:Thread-12, Priority:BelowNormal , Count:2
Name:Thread-1, Priority:Lowest , Count:0
Name:Thread-1, Priority:Lowest , Count:1
Name:Thread-1, Priority:Lowest , Count:2
Name:Thread-17, Priority:BelowNormal , Count:0
Name:Thread-17, Priority:BelowNormal , Count:1
Name:Thread-17, Priority:BelowNormal , Count:2
Name:Thread-16, Priority:BelowNormal , Count:0
Name:Thread-16, Priority:BelowNormal , Count:1
Name:Thread-16, Priority:BelowNormal , Count:2
Name:Thread-13, Priority:BelowNormal , Count:0
Name:Thread-13, Priority:BelowNormal , Count:1
Name:Thread-13, Priority:BelowNormal , Count:2
Name:Thread-9, Priority:Lowest , Count:0
Name:Thread-9, Priority:Lowest , Count:1
Name:Thread-9, Priority:Lowest , Count:2
Name:Thread-8, Priority:Lowest , Count:0
Name:Thread-8, Priority:Lowest , Count:1
Name:Thread-8, Priority:Lowest , Count:2
Name:Thread-5, Priority:Lowest , Count:0
Name:Thread-5, Priority:Lowest , Count:1
Name:Thread-5, Priority:Lowest , Count:2
Name:Thread-4, Priority:Lowest , Count:0
Name:Thread-4, Priority:Lowest , Count:1
Name:Thread-4, Priority:Lowest , Count:2
线程让步 Yield()
暂停当前正在执行的线程对象,并执行其他线程。Yield()做的是让当前运行线程回到可运行状态,以运行具有相同优先级的其他线程获得机会。但是,实际中无法保证Yield()达到让步目的,因为让步的线程还有可能被线程调度器再次选中。Yield()不会导致线程进入WaitSleepJoin状态。