var nums = Enumerable.Range(1,4).ToArray(); int total = 0; Parallel.For<int>( fromInclusive: 0, toExclusive: nums.Length, /* 陷阱 */ localInit: () => 1, body: (i, loopState, subtotal) => { return subtotal + nums[i]; }, localFinally: i => Interlocked.Add(ref total, i) ); Console.WriteLine("total={0}",total);
localInit會根據啓動的線程來調用屢次。若是隻啓用了一個線程,結果是11,二個是否2