class Program { static void Main(string[] args) { var count = 0; var taskList = new Task[10]; Stopwatch sp = new Stopwatch(); sp.Start(); // 不要意外复制。每个实例都是独立的。 SpinLock spinLock = new SpinLock(); for (int i = 0; i < taskList.Length; i++) { taskList[i] = Task.Run(() => { bool lock = false; for (int j = 0; j < 10_000_000; j++) { spinLock.Enter(ref _lock); count++; spinLock.Exit(); lock = false; } }); } sp.Stop(); Task.WaitAll(taskList); Console.WriteLine($"完成! 耗时:{sp.ElapsedTicks}"); Console.WriteLine($"结果:{count}"); } }