.NET线程是否从创建线程继承其优先级?
如果我跨越优先级高于父级的线程内的线程怎么办?
答案 0 :(得分:3)
简答:不。
你的前提需要重新审视;当你产生一个新线程时,它不被认为是在另一个线程“内部”。它只是添加到当前进程内所有运行线程的列表中。
答案 1 :(得分:1)
线程都是使用默认(普通)优先级创建的,必须明确设置优先级。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
System.Threading.ThreadStart ts = new System.Threading.ThreadStart(RunFirst);
System.Threading.Thread t = new System.Threading.Thread( ts);
t.Priority = System.Threading.ThreadPriority.BelowNormal;
t.Start();
System.Console.ReadLine();
}
public static void RunFirst()
{
System.Console.WriteLine("First Thread: " + System.Threading.Thread.CurrentThread.Priority.ToString());
System.Threading.ThreadStart ts = new System.Threading.ThreadStart(RunChild);
System.Threading.Thread t = new System.Threading.Thread(ts);
t.Start();
}
public static void RunChild()
{
System.Console.WriteLine("Child: " + System.Threading.Thread.CurrentThread.Priority.ToString());
}
}
}
输出:
First Thread: BelowNormal
Child: Normal