多线程排序法

多线程排序法

chatGPT给我改的多线程排序法
在这里插入图片描述using System.Collections.Concurrent; using System.Threading; ConcurrentBag<int> sortedList = new ConcurrentBag<int>(); void Sort() { int[] arr = {2, 6, 12, 8}; List<Thread> threads = new List<Thread>(); for (int i = 0; i < arr.Length; ++i) { Thread thread = new Thread(new ThreadStart(() => Run(arr[i]))); thread.Start(); threads.Add(thread); } foreach (var thread in threads) { thread.Join(); // 等待所有线程执行完成 } // 输出排序后的结果 foreach (var num in sortedList) { Console.Write(num + " "); } } void Run(int i) { Thread.Sleep(i); sortedList.Add(i); }

猜你喜欢

转载自blog.csdn.net/qq_31042143/article/details/130691107