Presentation is loading. Please wait.

Presentation is loading. Please wait.

System.Threading Process / Thread SD50 – C# & .NET Platform.

Similar presentations


Presentation on theme: "System.Threading Process / Thread SD50 – C# & .NET Platform."— Presentation transcript:

1 System.Threading Process / Thread SD50 – C# & .NET Platform

2 개요 Thread 란? 장점 단점 Processor Time을 할당하는 최소 단위 생성시 Process보다 저렴한 비용
동시에 여러 작업을 수행 가능 Thread간의 통신이 용이 단점 OS의 부하증가 Debugging의 복잡성 증가

3 Single Thread 수행시간 작업1 작업2 작업3

4 Multi Thread 수행시간 작업1 작업2 작업3

5 Context Switch 실행이 끝난 thread의 context를 저장하고 ready thread로 전환하는 과정
높은 priority를 가진 thread가 ready thread가 된 경우 실행중인 thread가 대기상태로 될 필요가 있는 경우

6 전형적인 Win32 Process 단일 Win32 프로세스 공유 데이터 쓰레드 A 쓰레드 B TLS 호출 스택 TLS

7 단일 .NET 프로세스 AppDomain A AppDomain B 단일 .NET 프로세스 공유 데이터 공유 데이터 쓰레드 A
TLS TLS TLS 호출 스택 호출 스택 호출 스택

8 System.Threading Namespace
멀티스레딩 지원 클래스와 인터페이스 제공 ThreadPool Class – 스레드 그룹관리 Timer Class – 일정시간뒤 호출 Mutex Class – 스레드간 상호배제를 위한 동기화 그 외 스레드 스케쥴링, Wait 통지, dead-lock 해지를 위한 클레스 제공

9 System.Threading.Thread Class
프로세스는 프로그램 코드의 실행 부여를 위한 하나이상의 스레드생성 가능 실행될 코드는 ThreadStart delegate로 지정 스레드가 존재하는 동안 스레드는 ThreadState로 정의되는 상태를 가진다. 스레드를 위하여 ThreadPriority로 정의되는 스케쥴링 우선 레벨을 지정할 수 있다.

10 System.Threading.Thread Class
public Thread( ThreadStart start ); start - A ThreadStart delegate that references the methods to be invoked when this thread begins executing. To schedule the thread for execution, call the Start method.

11 Thread Class Methods public static Thread CurrentThread {get;}
public static void Sleep(int); public void Suspend(); public void Resume(); Etc…

12 Demo System.Threading.Thread Class DzSimpleThread
Thread.CurrentThread.GetHashCode ()

13 실습과제 Thread Class를 이용하여, 화면에 초단위로 현재시간 표시와 동시에, 10초마다 새로운 스레드에서 메시지 박스 띄우기

14 System.Threading.Timer Class
Timer 와 연계된 메소드는 TimerCallback delegate 로 표현한다. 연계된 메소드는 기본스레드가 아닌 새로운 스레드를 자동 생성하여 실행한다. 타이머가 생성시, due time과 period를 지정 0, Infinite Change메소드로 위의 값 변경 또는 타이머를 disable 로 만들 수 있다. 더 이상 타이머가 필요없을때, Dispose 메소드로 타이머의 리소스를 해제

15 System.Threading.Timer Class
public Timer(TimerCallback, object, int, int); public Timer(TimerCallback, object, long, long); public Timer(TimerCallback, object, TimeSpan, TimeSpan); public Timer(TimerCallback, object, uint, uint);

16 Demo System.Threading.Timer Class DzSimpleTimer

17 실습과제 Timer Class 를 이용하여 10회동안 10초에 한번씩 화면에 메시지 박스 출력하기 (Form/Console)

18 Multi-Thread 멀티스레딩은 어플리케이션의 수행능력 향상 스레드간 공유데이터의 처리문제 발생
예측불가능 버그 발생 위험성 존재

19 쓰레드 동기화 lock(this) in C# System.Threading.Monitor
Monitor.Enter(this) / Monitor.Exit(this) Try{} finally{} 필수 왜냐하면, 진입스레드의 suspend = 무한루프 System.Threading.Interlocked 안전한 방법으로 변수의 값을 1씩 증가/감소

20 Demo 멀티스레드에서의 문제점 (동기화문제) DzMultiThreadProb

21 Demo 동기화 사용 예제 lock Keyword System.Threading.Monitor
System.Threading.Interlocked Critical-Section(임계영역) Mute X(상호배제) Semaphore(제안(최대) 스레드) Event(이벤트 발생시 들어감);

22 실습과제 멀티스레드를 이용한 예제

23 Thread Debugging System.Diagnostics.Debug Class - Debug.WriteLine
DebugView (

24 Demo Debug.WriteLine / DebugView 이용 예제 직접 설명하며 Coding

25 실습과제 스레드를 100개 만들어 스레드 이름에 인덱스 번호를 붙이고, 스레드 함수 내에서 Debug.WriteLine 을 이용하여 디버깅 정보 남기기


Download ppt "System.Threading Process / Thread SD50 – C# & .NET Platform."

Similar presentations


Ads by Google