본문 바로가기
[2-3]operating system/[2-3.2]Thread

스레드의 파라미터 전달

by 오늘도 빛나는 너에게 2020. 5. 13.
728x90

class Program

       {
              static void Main(string[] args)
              {
                     var sample = new ThreadSample(10);
                     var threadOne = new Thread(sample.CountNumbers);
                     threadOne.Name = "ThreadOne";
                     threadOne.Start();
                     threadOne.Join();
                     Console.WriteLine("--------------------------");
                     var threadTwo = new Thread(Count);
                     threadTwo.Name = "ThreadTwo";
                     threadTwo.Start(8);
                     threadTwo.Join();
                     Console.WriteLine("--------------------------");
                     var threadThree = new Thread(() => CountNumbers(12));
                     threadThree.Name = "ThreadThree";
                     threadThree.Start();
                     threadThree.Join();
                     Console.WriteLine("--------------------------");
                     int i = 10;
                     var threadFour = new Thread(() => PrintNumber(i));
                     i = 20;
                     var threadFive = new Thread(() => PrintNumber(i));
                     threadFour.Start();
                     threadFive.Start();
              }
              static void Count(object iterations)
              {
                     CountNumbers((int)iterations);
              }
              static void CountNumbers(int iterations)
              {
                     for (int i = 1; i <= iterations; i++)
                     {
                           Thread.Sleep(TimeSpan.FromSeconds(0.5));
                           Console.WriteLine("{0} prints {1}", Thread.CurrentThread.Name, i);
                     }
              }
              static void PrintNumber(int number)
              {
                     Console.WriteLine(number);
              }
              class ThreadSample
              {
                     private readonly int _iterations;
                     public ThreadSample(int iterations)
                     {
                           _iterations = iterations;
                     }
                     public void CountNumbers()
                     {
                           for (int i = 1; i <= _iterations; i++)
                           {
                                  Thread.Sleep(TimeSpan.FromSeconds(0.5));
                                  Console.WriteLine("{0} prints {1}", Thread.CurrentThread.Name, i);
                           }
                     }
              }
       }
 
 
설명: 메인 프로그램을 시작하면 먼저 반복 횟수를 제공하는 ThreadSample 클래스의 객채를 생성한다.
      그런 후에 이 객체의 CountNumbers 메소드와 함께 스레드를 실행한다.
      CountNumbers 메소드는 다른 스레드에서 실행하지만,객체 생성자에게 전달한 값인 숫자 1을 사용한다.
      따라서 동일한 간접적인 방법으로 다른 스레드에에게 반복 횟수를 전달만 했다.
      데이터를 전달하는 다른 방법은  다른스레드에게 전달할수 있는 다른 객체를 받는 Thread.Start 메소드를 사용하는것이다.
      이방식으로 작동하려면 다른 스레드에서 시작된 이 메소드가 object 타입의 파라미터 하나만 받아야 한다. 
      이옵션은 ThreadTow 스레드 생성으로 보여줬다.8을 객체로 Count 메소드에 전달하면,integer 타입으로 Casting 한다.
 
 
728x90

댓글