Thread là tiến trình nhẹ. Thread định nghĩa đường dẫn thực thi của chương trình. Tạo bằng cách extend class Thread, sau đó gọi Start() để bắt đầu child thread.
Ví dụ phổ biến: lập trình đồng thời (concurrent programming) trong OS hiện đại. Thread tiết kiệm CPU cycle, tăng hiệu suất.
Ví dụ
using System;
using System.Threading;
namespace Demo {
class Program {
public static void ThreadFunc() { Console.WriteLine("Child thread starts"); }
static void Main(string[] args) {
ThreadStart childref = new ThreadStart(ThreadFunc);
Console.WriteLine("In Main: Creating the Child thread");
Thread childThread = new Thread(childref);
childThread.Start();
Console.ReadKey();
}
}
}
Kết quả
In Main: Creating the Child thread Child thread starts