티스토리 뷰

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);
       }
   }
}

'Code > C#' 카테고리의 다른 글

[C#] Thread 샘플 코드 4  (0) 2007.12.08
[C#] Thread & FileStream 사용 예제  (0) 2007.12.07
[C#] Thread 샘플 코드 2  (0) 2007.12.07
[C#] Thread 샘플 코드 1  (0) 2007.12.07
[C#] Extension Methods 샘플 코드  (0) 2007.12.02
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함