マルチスレッドソート

マルチスレッドソート

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
おすすめ