C#中的线程概念

use*_*487 6 c# multithreading

我是C#的新手,还在学习线程概念.我写了一个程序如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ConsoleApplication17
{
  class Program
  {
    static void Main(string[] args)
    {
        System.Threading.ThreadStart th1 = new System.Threading.ThreadStart(prnt);
        Thread th = new Thread(th1);
        th.Start();
        bool t = th.IsAlive;
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine(i + "A");
        }
    }

    private static void prnt()
    {
        for (int i = 0; i < 10; i ++)
        {
            Console.WriteLine(i + "B");
        }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我期待输出如下: -

1A
2A
1B
3A
2B...
Run Code Online (Sandbox Code Playgroud)

因为我相信主线程和新创建的线程都应该同时执行.

但输出是有序的,首先prnt调用该函数然后Main执行循环方法.

Gia*_*olo 3

也许 10 次循环不足以看到两个线程同时运行。使用更长的循环或在循环迭代中放置 Thread.Sleep (100)。启动一个线程是相当昂贵的。可能发生的情况是,由于线程启动所需的时间,主循环在线程循环启动之前执行