单元测试中经常需要对[ThreadStatic]以及一些互斥环境的处理进行简单验证,启动并行处理方法很多,这里介绍一个简单的方法。
static void Parallel(params ThreadStart[] actions)
{
List<Thread> threads = actions.Select(a => new Thread(a)).ToList();
threads.ForEach(t => t.Start());
threads.ForEach(t => t.Join());
}
{
List<Thread> threads = actions.Select(a => new Thread(a)).ToList();
threads.ForEach(t => t.Start());
threads.ForEach(t => t.Join());
}