List中的N为什么会有重复?这是Framework中的bug吗?(我的电脑4核i5 7500)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp
{
class Program
{
//static AutoResetEvent mre = new AutoResetEvent(true);
static ManualResetEventSlim mre = new ManualResetEventSlim(true);
static List<string> list = new List<string>();
static void Main(string[] args)
{
Task[] tasks = new Task[10000];
for (int i = 0; i < 10000; i++)
{
tasks[i] = Task.Factory.StartNew(Test);
//tasks[i].ContinueWith(t => Set());
}
Task.WaitAll(tasks);
var q = from t in list
group t by t into g
where g.Count() >1
select new
{
g.Key,
Count = g.Count()
};
q.ToList().ForEach((t) => Console.WriteLine(t.Key + "---" + t.Count));
Console.WriteLine("OK");
}
//static void Set()
//{
// mre.Set();
//}
static int n = 0;
static string Test()
{
try
{
mre.Wait();
mre.Reset();
Interlocked.Increment(ref n);
var s = n.ToString();
list.Add(s);
return s;
}
finally
{
mre.Set();
}
}
}
}