C#. 쓰레드(Thread)

Unity3D/C# 2019. 4. 1. 19:04
반응형

프로세스(Process) 와 쓰레드 (Thread)
프로세스는 실행 파일이 실행되어 메모리에 적재된 인스턴스입니다. 
운영체제는 여러가지 프로세스를 동시에 실행할 수 있는 능력을 갖추고 있습니다. 
쓰레드는 운영체제가 CPU 시간을 할당하는 기본 단위인데, 프로세스는 하나 이상의 쓰레드로 구성됩니다.

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
 
namespace Thread_Test_00
{
    class Program
    {
        static void Main(string[] args)
        {
            Account account = new Account();
            Thread ATM = new Thread(new ThreadStart(account.Withdraw3));
            Thread Phone = new Thread(new ThreadStart(account.Withdraw3));
            Thread Internet = new Thread(new ThreadStart(account.Withdraw3));
 
            Console.WriteLine("ATM");
            ATM.Start();
            Console.WriteLine("Phone");
            Phone.Start();
            Console.WriteLine("Internet");
            Internet.Start();
 
            Console.ReadKey();
 
        }
    }
}
 
 
 
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
77
78
79
80
81
82
83
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace Thread_Test_00
{
    public class Account
    {
        private readonly object lockObj = new object();
        private bool isLocked = false;  //다른쓰레드가 공유자원을 사용하고 있는지 판별
        public int Money { get; set; }
 
        public Account()
        {
            this.Money = 1000;
        }
 
        public void Withdraw()
        {
            lock (this.lockObj)
            {
                if (this.Money <= 0)
                {
                    Console.WriteLine("잔액이 모자랍니다.");
                }
                else
                {
                    this.Money -= 1000;
                }
            }
        }//withdraw
 
        public void Withdraw2()
        {
            Monitor.Enter(this.lockObj);
            try
            {
                if (this.Money <= 0)
                {
                    Console.WriteLine("잔액이 모자랍니다.");
                }
                else
                {
                    this.Money -= 1000;
                }
            }
            finally
            {
                Monitor.Exit(this.lockObj);
            }
        }//withdraw2
 
        public void Withdraw3()
        {
            lock (this.lockObj)
            {
                while (this.isLocked)
                    Monitor.Wait(this.lockObj);
 
                this.isLocked = true;
 
                if (this.Money <= 0)
                {
                    Console.WriteLine("잔액이 모자랍니다.");
                }
                else
                {
                    this.Money -= 1000;
                }
 
                this.isLocked = false;  //다른 쓰레드를 깨움 
                //깨어난 쓰레드들은 while의 조건검사를 통해 wait()을 호출할지를 결정함 
 
                Monitor.Pulse(this.lockObj);
            }
        }//withdraw3 
    }
}
 
 
 
 

참고: https://bit.ly/2OmVlHh

반응형

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

referencesource microsoft  (0) 2019.04.02
IEnumerator, IEnumerable 상속받은 Inventory 구현 (인덱서)  (0) 2019.04.01
컬렉션(C#)  (0) 2019.03.28
2019.03.26 과제 (와우 케릭터 만들기 C#콘솔)  (0) 2019.03.27
enum과 비트마스크  (0) 2019.03.26
: