스레드 풀의 경우 실행을 위해 2개 이상의 함수와 대기열 메서드를 생성합니다.
먼저 −
와 같은 메서드를 만듭니다.public void one(object o) { for (int i = 0; i <= 3; i++) { Console.WriteLine("One executed"); } }
같은 방법으로 더 많은 메서드를 만든 다음 ThreadPool.QueueUserWorkItem을 사용합니다. 실행을 위해 메소드를 큐에 넣으려면 -
Demo d = new Demo(); for (int i = 0; i < 3; i++) { ThreadPool.QueueUserWorkItem(new WaitCallback(d.one)); ThreadPool.QueueUserWorkItem(new WaitCallback(d.two)); ThreadPool.QueueUserWorkItem(new WaitCallback(d.three)); }
예시
다음 C# 코드를 실행하여 스레드 풀을 만들 수 있습니다.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; class Demo { public void one(object o) { for (int i = 0; i <= 3; i++) { Console.WriteLine("One executed"); } } public void two(object o) { for (int i = 0; i <= 3; i++) { Console.WriteLine("Two executed"); } } public void three(object o) { for (int i = 0; i <= 3; i++) { Console.WriteLine("Three executed"); } } static void Main() { Demo d = new Demo(); for (int i = 0; i < 3; i++) { ThreadPool.QueueUserWorkItem(new WaitCallback(d.one)); ThreadPool.QueueUserWorkItem(new WaitCallback(d.two)); ThreadPool.QueueUserWorkItem(new WaitCallback(d.three)); } Console.Read(); } }
출력
Two executed Two executed Two executed Two executed Two executed Two executed Two executed One executed One executed One executed One executed One executed Two executed Two executed Three executed Three executed Two executed One executed Three executed Two executed Three executed One executed One executed One executed