아래는 간단한 Thread 샘플 코드이다.
난수를 발생시키는 2개의 Thread가 있고, 발생한 난수만큼 Sleep 한다.
난수를 발생시키는 2개의 Thread가 있고, 발생한 난수만큼 Sleep 한다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace SampleThread
{
class Program
{
static void Main(string[] args)
{
myRandom = new Random();
Thread t1 = new Thread(new ThreadStart(method1));
t1.Start();
Thread t2 = new Thread(new ThreadStart(method2));
t2.Start();
}
private static void method1()
{
for (int i = 0; i < 5; i++)
{
r = myRandom.Next(6) * 1000;
Console.WriteLine("[{0,-10}]", r);
Thread.Sleep(r);
}
}
private static void method2()
{
for (int i = 0; i < 5; i++)
{
r = myRandom.Next(6) * 1000;
Console.WriteLine("[{0,10}]", r);
Thread.Sleep(r);
}
}
private static Random myRandom;
private static int r;
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace SampleThread
{
class Program
{
static void Main(string[] args)
{
myRandom = new Random();
Thread t1 = new Thread(new ThreadStart(method1));
t1.Start();
Thread t2 = new Thread(new ThreadStart(method2));
t2.Start();
}
private static void method1()
{
for (int i = 0; i < 5; i++)
{
r = myRandom.Next(6) * 1000;
Console.WriteLine("[{0,-10}]", r);
Thread.Sleep(r);
}
}
private static void method2()
{
for (int i = 0; i < 5; i++)
{
r = myRandom.Next(6) * 1000;
Console.WriteLine("[{0,10}]", r);
Thread.Sleep(r);
}
}
private static Random myRandom;
private static int r;
}
}
'Code > C#' 카테고리의 다른 글
| [C#] Thread 샘플 코드 3 (0) | 2007.12.07 |
|---|---|
| [C#] Thread 샘플 코드 2 (0) | 2007.12.07 |
| [C#] Extension Methods 샘플 코드 (0) | 2007.12.02 |
| [C#] 중복되지 않는 난수 발생 샘플 코드 (0) | 2007.11.29 |
| [C#] Jagged Array 선언 방법 (0) | 2007.11.29 |