多线程排序法
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); }