스레드는 경량 프로세스입니다. 스레드는 프로그램의 실행 경로로 정의됩니다. 스레드는 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