线程是轻量级进程。线程被定义为程序的执行路径。通过扩展Thread类来创建线程。然后,扩展的Thread类将调用该Start()方法以开始执行子线程。
线程示例:线程使用的一个常见示例是现代操作系统对并发编程的实现。使用线程可以节省CPU周期,并提高应用程序的效率。
以下是显示如何创建线程的示例。
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();
}
}
}输出结果
In Main: Creating the Child thread Child thread starts