1. 利用Thread start启动线程
ThreadStart表示执行线程的方法
ThreadStart(delegate(){})
public static void testThread()
{
int Max_Thread_Count = 10;
long currentThreadCount = 0;
for(var i = 0;i < 100;i ++)
{
var ii = i;
while(true)
{
if (Interlocked.Read(ref currentThreadCount) < Max_Thread_Count)
break;
Thread.Sleep(1000);
}
Interlocked.Increment(ref currentThreadCount);
new Thread(new ThreadStart(delegate()
{
Thread.Sleep(1000);
Console.WriteLine("Current position is " + ii);
Thread.Sleep(1000);
Interlocked.Decrement(ref currentThreadCount);
})).Start();
}
while (true)
{
Console.WriteLine("Running thread count " + currentThreadCount);
if (Interlocked.Read(ref currentThreadCount) == 0)
break;
Thread.Sleep(1000);
}
}

本文探讨了C#中利用Thread Start和ThreadPool QueueUserWorkItem创建多线程的方法,强调了Thread Start的独立性和 ThreadPool的效率优势。文中建议,除非需要特定线程属性或长运行任务,否则推荐使用ThreadPool。此外,还提及.NET 4提供的Task、Parallel类和PLINQ作为并发处理的现代选择。
最低0.47元/天 解锁文章
2482

被折叠的 条评论
为什么被折叠?



