您的位置:首页 > 编程语言 > C#

C# 线程优先级和线程让步

2014-07-25 17:56 417 查看
线程优先级

Priority
Summary
Lowest = 0The System.Threading.Thread can be scheduled after threads with any other priority
BelowNormal = 1The System.Threading.Thread can be scheduled after threads with Normal priority and before those with Lowest priority.
Normal = 2The System.Threading.Thread can be scheduled after threads with AboveNormal priority and before those with BelowNormal priority. Threads have Normal priority by default.
AboveNormal = 3The System.Threading.Thread can be scheduled after threads with Highest priority and before those with Normal priority.
Highest = 4The 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状态。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  线程优先级