Code/C#

[C#] Thread 샘플 코드 3

Hide Code 2007. 12. 7. 20:05
Thread에 관한 3번째 샘플 코드이다.

이 샘플 코드는 Thread를 3개 실행 시키는 것이다.



using System;
using System.Threading;

public class NameUsingThread
{
   private int time;
   private Thread thread;

   public NameUsingThread(String n, int t)
   {
       time = t;
       thread = new Thread(new ThreadStart(Run));
       thread.Name = n;
       thread.Start();
   }

   public void Run()
   {
       for (int i = 1; i <= 5; i++)
       {
           Console.WriteLine(thread.Name + " " + i);
           Thread.Sleep(time);
       }
   }

   public static void Main()
   {
       NameUsingThread bonnie = new NameUsingThread("Bonnie", 500);
       NameUsingThread clyde = new NameUsingThread("Clyde", 1000);

       Thread.CurrentThread.Name = "Main";
       
       for (int i = 1; i <= 5; i++)
       {
           Console.WriteLine(Thread.CurrentThread.Name + " " + i);
           Thread.Sleep(1500);
       }
   }
}