前言
什么时候才能像你一样,拥有可以为之奋斗一生的事业。年少时为工作操碎了的那颗心,却在最后才发现,原来自己连为之奋斗的资格都没有。
也许这就是人生。时光的魔力总是让人惊叹,它总会磨平一切棱角,磨尽所有热情,一点一点的将自己打磨成被需要的形状。它也是、被遗忘的形状。
于是你踌躇于杨柳岸,闻晓风、赏残月;于是你 落魄在橱窗前,观行人、听落雪;惜 抚凌云以杨意;盼 奏流水之钟期。于是你回望了少许刚起步不久的人生,赞生命之可贵;于是你驻足了许久将要到来的挑战,叹愈战 而愈坚。
本文简单的总结了 C# 中 线程与线程池的常用操作。
开始
Thread
新建控制台应用,并替换Program的代码即可;
示例程序(官方文档示例程序改版):
class Program
{
public static void ThreadProc()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("ThreadProc: {0}", i);
Thread.Sleep(100);
}
}
public static void Main()
{
Console.WriteLine("Main thread: Start a second thread.");
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
for (int i = 0; i < 4; i++)
{
Console.WriteLine("Main thread: Do some work.");
Thread.Sleep(100);
}
Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
t.Join();
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
Console.ReadLine();
}
}
输出像下面这样:
Main thread: Start a second thread.
Main thread: Do some work.
ThreadProc: 0
Main thread: Do some work.
ThreadProc: 1
Main thread: Do some work.
ThreadProc: 2
Main thread: Do some work.
ThreadProc: 3
Main thread: Call Join(), to wait until ThreadProc ends.
ThreadProc: 4
ThreadProc: 5
ThreadProc: 6
ThreadProc: 7
ThreadProc: 8
ThreadProc: 9
Main thread: ThreadProc.Join has returned. Press Enter to end program.
subThread.Join() 表示将线程subThread附加到当前线程,即当前线程会等待subThread线程结束后再结束.
带参数使用Thread
public static void Main()
{
Console.WriteLine("Main thread: Start a second thread.");
Thread t = new Thread(ThreadProcWithParam);
t.Start(1000); //线程t将会执行 ThreadProcWithParam(1000)
for (int i = 0; i < 4; i++)
{
Console.WriteLine("Main thread: Do some work.");
Thread.Sleep(100);
}
Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
t.Join();
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
Console.ReadLine();
}
public static void ThreadProcWithParam(object obj)
{
int interval = (int)obj;
for (int i = 0; i < 10; i++)
{
Console.WriteLine("ThreadProc: {0}", i);
Thread.Sleep(interval);
}
}
另外一种带参Thread调用方式 是替换下面代码
Thread t = new Thread(ThreadProcWithParam);
t.Start(1000); //线程t将会执行 ThreadProcWithParam(1000)
为
Thread t = new Thread(()=> { ThreadProcWithParam(1000); });
t.Start();
ThreadPool
示例代码:
public static void Main()
{
ThreadPool.QueueUserWorkItem(ThreadProc);
Console.WriteLine("Main thread does some work, then sleeps.");
for(int i = 0; i < 10; i++)
{
Console.WriteLine("Main thread output " + i);
Thread.Sleep(100);
}
Thread.Sleep(1000);
Console.WriteLine("Main thread exits.");
Console.Read();
}
public static void ThreadProc(object stateInfo)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("ThreadProc: {0}", i);
Thread.Sleep(100);
}
}
带参数:
public static void Main()
{
ThreadPool.QueueUserWorkItem(ThreadProcWithParam,500);
Console.WriteLine("Main thread does some work, then sleeps.");
for(int i = 0; i < 10; i++)
{
Console.WriteLine("Main thread output " + i);
Thread.Sleep(100);
}
Thread.Sleep(1000);
Console.WriteLine("Main thread exits.");
Console.Read();
}
public static void ThreadProc(object stateInfo)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("ThreadProc: {0}", i);
Thread.Sleep(100);
}
}
或者将
ThreadPool.QueueUserWorkItem(ThreadProcWithParam,500);
替换为
ThreadPool.QueueUserWorkItem((obj) => { ThreadProcWithParam(500); });
结束
对于 .Net 中多线程的处理,官方的建立是使用 任务并行库 (TPL) 和并行 LINQ (PLINQ) https://docs.microsoft.com/zh-cn/dotnet/standard/threading/threads-and-threading?view=netcore-3.1
TPL 简单示例:
// Sequential version
foreach (var item in sourceCollection)
{
Process(item);
}
// Parallel equivalent
Parallel.ForEach(sourceCollection, item => Process(item));
//尽可能的并发执行actions,且最大线程数为5
Parallel.Invoke(new ParallelOptions() { MaxDegreeOfParallelism = 5, }, actions.ToArray());
参考文档
https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.thread?view=netcore-3.1
https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.threadpool?view=netcore-3.1
https://docs.microsoft.com/zh-cn/dotnet/standard/threading/threads-and-threading?view=netcore-3.1
https://www.cnblogs.com/scmail81/p/9503266.html