노력과 삽질 퇴적물

C#: 문법 스캔 (2) 본문

프로그래밍note/언어. C# 계열

C#: 문법 스캔 (2)

MTG 2021. 7. 16. 00:10

 
목차
1.
 스레딩 기본 제어
2.
 스레드풀






1. 스레딩 기본 제어


1) 생성 및 기본 사용.
> [#웹 컴파일]
> Thread형 오브젝트의 프로퍼티로 IsBackground를 설정후 start하는걸로 포그라운드/백그라운드 처리가능.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Text.RegularExpressions;
using System.Threading;
 
namespace Rextester
{
    public class MyThreadClass
    {
        public static int SLEEP_MS = 10000;//10초.
        public static string TIME_FORM = "HH:mm:ss.fff";
        
        public void InstanceFunc(/*함수 인자 및 델레기이트 가능*/)
        {
            try{
                Console.WriteLine("[{0}] Thread {1}", DateTime.Now.ToString(TIME_FORM), Thread.CurrentThread.Name);
                Thread.Sleep(SLEEP_MS);
            }
            catch (ThreadInterruptedException) {
                Console.WriteLine("[{0}] ThreadInterruptedException, '{1}'", DateTime.Now.ToString(TIME_FORM), Thread.CurrentThread.Name);
            }
            catch (ThreadAbortException) {
                 Console.WriteLine("[{0}] ThreadAbortException, '{1}'", DateTime.Now.ToString(TIME_FORM), Thread.CurrentThread.Name);
            }
            
            Console.WriteLine("[{0}] FIN, {1}", DateTime.Now.ToString(TIME_FORM), Thread.CurrentThread.Name);
            /*
            Sleep이 호출되면 지정된 시간만큼 다른 스레드에게 리소스 양도.
            한 스레드가 다른 스레드에서 Thread.Sleep(정적 매소드)는 호출 불가.
            */
        }
        public static void StaticFunc(/*함수 인자 및 델레기이트 가능*/)
        {
            try{
                Console.WriteLine("[{0}] Thread {1}", DateTime.Now.ToString(TIME_FORM), Thread.CurrentThread.Name);
                Thread.Sleep(SLEEP_MS);
            }
            catch (ThreadInterruptedException) {
                Console.WriteLine("[{0}] ThreadInterruptedException, '{1}'", DateTime.Now.ToString(TIME_FORM), Thread.CurrentThread.Name);
            }
            catch (ThreadAbortException) {
                 Console.WriteLine("[{0}] ThreadAbortException, '{1}'", DateTime.Now.ToString(TIME_FORM), Thread.CurrentThread.Name);
            }
            
            Console.WriteLine("[{0}] FIN, {1}", DateTime.Now.ToString(TIME_FORM), Thread.CurrentThread.Name);
        }
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            MyThreadClass threadObj = new MyThreadClass();
            Thread threadForIns = null;
            Thread threadForStatic = null;
            threadForIns = new Thread(new ThreadStart(threadObj.InstanceFunc));            //함수 인자X
            //threadForIns = new Thread(new ThreadStart(threadObj.InstanceFunc(...)));
            threadForIns.Name = "InstanceFunc";
            threadForIns.Start();
 
            Console.WriteLine("[{0}] before Thread.Sleep", DateTime.Now.ToString(MyThreadClass.TIME_FORM));
            Thread.Sleep(MyThreadClass.SLEEP_MS / 5);
            Console.WriteLine("[{0}] before Interrupt", DateTime.Now.ToString(MyThreadClass.TIME_FORM));
            threadForIns.Interrupt();//wake쯤?
 
            //정적 함수이니 [클래스명.함수명]호출.
            threadForStatic = new Thread(new ThreadStart(MyThreadClass.StaticFunc));        //함수 인자X
            //threadForStatic = new new Thread(new ThreadStart(MyThreadClass.StaticFunc(...)));
            threadForStatic.Name = "StaticFunc";
            threadForStatic.Start();
            
            Console.WriteLine("[{0}] before Thread.Sleep", DateTime.Now.ToString(MyThreadClass.TIME_FORM));
            Thread.Sleep(MyThreadClass.SLEEP_MS / 5);
            Console.WriteLine("[{0}] before Abort", DateTime.Now.ToString(MyThreadClass.TIME_FORM));
            threadForStatic.Abort(); //스레드 제거
        }
    }
}
cs

> 실행결과

[15:44:59.345] before Thread.Sleep
[15:44:59.360] Thread InstanceFunc
[15:45:01.377] before Interrupt
[15:45:01.377] ThreadInterruptedException, 'InstanceFunc'
[15:45:01.377] FIN, InstanceFunc
[15:45:01.377] Thread StaticFunc
[15:45:01.377] before Thread.Sleep
[15:45:03.393] before Abort
[15:45:03.410] ThreadAbortException, 'StaticFunc'
 
 
2) 타이머
> System.Threading.Timer과 System.Timers.Timer 2가지가 존재.
[#예제, System.Timers.Timer] //Elapsed에 넣는 static함수는 Task도 허용.
[#예제, System.Threading.Timer] //TimerCallback을 이용한 델리게이트





2. 스레드 풀

1) 최소&최대
> 최대: 스레드 풀에 대기 시킬 작업수는 메모리가 관건이나,
CPU코어당-1개 이상의 스레드
                           1 스레드-최대 N개의 작업 스레드.
> ThreadPool.SetMaxThreads(), ThreadPool.SetMinThreads()로 작업 스레드 갯수 조절.


2) 세부 항목

> 스레드 및 스레딩 사용






기타. 참조자료

1) 한국어
C# 프로그래밍 배우기 (Learn C# Programming)
 
> 관리되는 스레딩 기본사항
> 스레드 및 스레딩 사용
> 스레딩 개체 및 기능
 




기타. 변경이력

일자
 변경이력
 2021-03-28  초안 작성.