给一个很有趣的进程并发性的小例子:
static void Main(string[] args) { new Thread(WriteX).Start(); new Thread(WriteY ).Start(); WriteZ(); Console.ReadKey(); } static void WriteX() { Console.WriteLine($"线程编号X:{Thread.CurrentThread.ManagedThreadId}"); for (int i = 0; i < 1000; i++) Console.Write("x"); } static void WriteY() { Console.WriteLine($"线程编号Y:{Thread.CurrentThread.ManagedThreadId}"); for (int i = 0; i < 1000; i++) Console.Write("y"); } static void WriteZ() { Console.WriteLine($"线程编号Z:{Thread.CurrentThread.ManagedThreadId}"); for (int i = 0; i < 1000; i++) Console.Write("z"); } }
一共开了三个进程,分别打印X, Y,Z,看运行结果(每次都是随机的)
时间: 2024-10-06 04:57:51