多執行序(Multithreading)是一種在同一時間內執行多個任務的程式設計技術。 在 C# 中,多執行序的實現通常透過 System.Threading 命名空間提供的類別和方法。 以下是一些基本的多執行序概念和相關的 C# 類別:
- 基本概念:
- 執行緒(Thread):
執行緒是程式中執行的最小單位,它代表一條指令序列的執行路徑。
一個應用程式可以同時執行多個執行緒,每個執行緒獨立運作。
- 多執行緒:
多執行緒是同一時間內執行多個執行緒的機制,提高了系統的效能和反應時間。
- Thread 類別:
Thread 類別是 C# 中用於建立和控制執行緒的主要類別。
- ThreadPool 類別:
ThreadPool 類別提供了一個可以重複使用的執行緒池,可以用於執行排程的任務。
- 鎖(Locking):
在多執行緒環境中,當多個執行緒同時存取共享資源時可能發生競爭條件。 為了避免這種情況,可以使用 lock 關鍵字來確保一次只有一個執行緒可以進入被鎖定的區塊。
範例一:Thread 非鎖定式
using System;
using System.Threading;
class Program
{
static void Main()
{
Thread thread = new Thread(DoWork);// 建立新執行緒
thread.Start();// 啟動執行緒
for (int i = 0; i < 5; i++)// 主執行緒繼續執行其他工作
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Main Thread: {i * 10}");
Thread.Sleep(1000);
}
}
static void DoWork()
{
for (int i = 0; i < 5; i++)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine($"Worker Thread: {i}");
Thread.Sleep(300);
}
}
}
執行結果

範例二:Thread 鎖定式
using System;
using System.Threading;
class Program
{
private static readonly object consoleLock = new object();
static void Main()
{
Thread thread = new Thread(DoWork);// 建立新執行緒
thread.Start();// 啟動執行緒
for (int i = 0; i < 5; i++)// 主執行緒繼續執行其他工作
{
lock (consoleLock)//鎖住執行序確保文字顏色
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Main Thread: {i * 10}");
}
Thread.Sleep(1000);
}
}
static void DoWork()
{
for (int i = 0; i < 5; i++)
{
lock (consoleLock)//鎖住執行序確保文字顏色
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine($"Worker Thread: {i}");
}
Thread.Sleep(300);
}
}
}
執行結果

範例三:Task 執行序可被中斷
using System;
using System.Threading.Tasks;
class Program
{
static void Main()
{
// 使用 Task.Run 建立新工作
Task task = Task.Run(() => DoWork());
// 主執行緒繼續執行其他工作
for (int i = 0; i < 5; i++)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Main Thread: {i * 10}");
Task.Delay(1000).Wait();
}
// 等待工作完成
task.Wait();
}
static void DoWork()
{
for (int i = 0; i < 5; i++)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine($"Worker Task: {i}");
Task.Delay(1000).Wait();
}
}
}
執行結果

範例四:Task 鎖定執行序不被中斷
using System;
using System.Threading.Tasks;
class Program
{
private static readonly object consoleLock = new object();
static void Main()
{
// 使用 Task.Run 建立新工作
Task task = Task.Run(() => DoWork());
// 主執行緒繼續執行其他工作
for (int i = 0; i < 5; i++)
{
lock (consoleLock)//鎖住執行序確保文字顏色
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Main Thread: {i * 10}");
}
Task.Delay(1000).Wait();
}
// 等待工作完成
task.Wait();
}
static void DoWork()
{
for (int i = 0; i < 5; i++)
{
lock (consoleLock)//鎖住執行序確保文字顏色
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine($"Worker Task: {i}");
}
Task.Delay(1000).Wait();
}
}
}
執行結果

範例五:非順序觸發執行序
using System;
using System.Threading.Tasks;
class Program
{
static void Main()
{
// 使用 Parallel.ForEach 並行處理集合元素
Parallel.ForEach(new[] { 1, 2, 3, 4, 5 }, item =>
{
Console.WriteLine($"Processed item: {item}");
Task.Delay(300).Wait();
});
}
}
執行結果

範例六:依序觸發執行序
using System;
using System.Threading.Tasks;
class Program
{
static void Main()
{
var items = new[] { 1, 2, 3, 4, 5 };
foreach (var item in items)
{
Task.Run(() =>
{
Console.WriteLine($"Processed item: {item}");
System.Threading.Thread.Sleep(300);
}).Wait();
}
}
}
執行結果

|