我是一个完全不懂线程的新手,正在尝试理解基础知识。我有这段使用异步委托调用的代码:
static void Main(string[] args)
{
Action<Thread, string> action = (mainThread, name) =>
{
Thread.CurrentThread.Name = name;
Thread.CurrentThread.IsBackground = false;
Console.WriteLine("Thread {0} starts", Thread.CurrentThread.Name);
while (true)
{
var input = Console.ReadLine();
Console.WriteLine("Thread {0} catches an input. User's input is: \"{1}\""
+"\nMain thread is alive = {2}",
Thread.CurrentThread.Name, input, mainThread.IsAlive);
}
};
action.BeginInvoke(Thread.CurrentThread,"First", null, null);
action.BeginInvoke(Thread.CurrentThread, "Second", null, null);
Thread.Sleep(2000);
}
无论我做什么,输出结果总是按照以下顺序:第一 -> 第二 -> 第一 -> 第二 -> 第一...
如我所知,输出顺序应该是不确定的。但在这种情况下它是确定的。线程队列是如何处理的?我漏掉了什么?请解释。