[C#] Thread 실습
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading;
namespace MorTest
{
public class Number
{
String name;
public Number(String na)
{
name = na;
}
public String Name
{
get { return name; }
set { name = value }
}
public virtual void Print()
{
}
}
public class JungSu : Number
{
public JungSu(String na)
: base(na)
{
this.Name = na;
}
public override void Print()
{
Console.WriteLine("정수 : {0}", this.Name);
}
}
public class MunJa : Number
{
public MunJa(String na)
: base(na)
{
this.Name = na;
}
public override void Print()
{
Console.WriteLine("문자열: {0}", this.Name);
}
}
public class Document : IEnumerable
{
public List<Number> MyList = new List<Number>();
String password = String.Empty;
String[] pass = { "h", "e", "l", "l", "o" };
Random ho = new Random();
Random r = new Random();
Number sn = new Number(null);
public void Delete()
{
while (!stop)
{
int key = r.Next(0, MyList.Count);
MyList.RemoveAt(key);
Console.WriteLine("{0} 번째 삭제", key);
Thread.Sleep(1000);
}
}
public void Add()
{
while (!stop)
{
Thread.Sleep(1000);
password = pass[ho.Next(5)];
Console.WriteLine("문자 : {0}", password);
sn = new JungSu(password);
MyList.Add(sn);
}
}
public void Add2()
{
while (!stop)
{
Thread.Sleep(1000);
String nn = r.Next(100).ToString();
Console.WriteLine("정수 : {0}", nn);
sn = new MunJa(nn);
MyList.Add(sn);
}
}
private volatile bool stop;
public void Stop()
{
stop = true
}
public void Start()
{
stop = false
}
public IEnumerator GetEnumerator()
{
return MyList.GetEnumerator();
}
public String SelectMenu()
{
// Console.WriteLine("1. 2개 쓰레드 돌리기");
// Console.WriteLine("2. 2개 돌리고 1개지우기");
// Console.WriteLine("3. 출력");
// Console.WriteLine("0. 종료");
String ss = Console.ReadLine();
return ss;
}
public void View()
{
foreach (Number per in MyList)
{
per.Print();
}
}
}
public class Program
{
static void Main(string[] args)
{
Document Docu = new Document();
Thread DocumentThreadA = new Thread(new ThreadStart(Docu.Add));
DocumentThreadA.Name = "A"
Thread DocumentThreadB = new Thread(new ThreadStart(Docu.Add2));
DocumentThreadB.Name = "B"
Thread DocumentThreadC = new Thread(new ThreadStart(Docu.Delete));
DocumentThreadC.Name = "C"
///////////////////////////
//쓰레드 시작
DocumentThreadA.Start();
DocumentThreadB.Start();
DocumentThreadC.Start();
///////////////////////////
// 쓰레드 상태를 일시정지
DocumentThreadA.Suspend();
DocumentThreadB.Suspend();
DocumentThreadC.Suspend();
String key;
while ((key = Docu.SelectMenu()) != "0")
{
switch (key)
{
case "1":
Docu.Start();
DocumentThreadA.Resume();
DocumentThreadB.Resume();
break ;
case "2":
DocumentThreadC.Resume();
break;
case "3":
DocumentThreadA.Suspend();
DocumentThreadB.Suspend();
DocumentThreadC.Suspend();
Docu.Stop();
Docu.View();
break
case "0":
break
}
}
}
}
}
'프로그래밍 > Language C#' 카테고리의 다른 글
[C#] Console.Write & WriteLine 차이점 (0) | 2013.05.31 |
---|---|
[C#] DB 연동 (0) | 2011.12.23 |
[C#] Thread 생성 (0) | 2011.11.16 |
[C#] 전화번호부 2탄 (List,IEnumberable 적용,클래스 상속) (0) | 2011.11.14 |
[C#] 전화번호 저장 (0) | 2011.11.14 |
댓글
이 글 공유하기
다른 글
-
[C#] Console.Write & WriteLine 차이점
[C#] Console.Write & WriteLine 차이점
2013.05.31 -
[C#] DB 연동
[C#] DB 연동
2011.12.23 -
[C#] Thread 생성
[C#] Thread 생성
2011.11.16 -
[C#] 전화번호부 2탄 (List,IEnumberable 적용,클래스 상속)
[C#] 전화번호부 2탄 (List,IEnumberable 적용,클래스 상속)
2011.11.14