■ 스레드 안전 우선 순위 큐를 만드는 방법을 보여준다.
▶ ThreadSafeSortedList.cs
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
using System; using System.Collections.Generic; namespace TestProject { /// <summary> /// 스레드 안전 정렬 리스트 /// </summary> /// <typeparam name="TItemKey">항목 키 타입</typeparam> /// <typeparam name="TItem">항목 타입</typeparam> public class ThreadSafeSortedList<TItemKey, TItem> : SortedList<TItemKey, TItem> { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Protected #region Field /// <summary> /// 동기 객체 /// </summary> protected object syncObject; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 카운트 - Count /// <summary> /// 카운트 /// </summary> public new int Count { get { lock(this.syncObject) { return base.Count; } } } #endregion #region 인덱서 - this[itemKey] /// <summary> /// 인덱서 /// </summary> /// <param name="itemKey">항목 키</param> /// <returns>항목</returns> public new TItem this[TItemKey itemKey] { get { lock(this.syncObject) { return base[itemKey]; } } set { lock(this.syncObject) { base[itemKey] = value; } } } #endregion #region 인덱서 - this[itemKeyIndex] /// <summary> /// 인덱서 /// </summary> /// <param name="itemKeyIndex">항목 키 인덱스</param> /// <returns>항목</returns> public TItem this[int itemKeyIndex] { get { lock(this.syncObject) { TItemKey itemKey = base.Keys[itemKeyIndex]; return base[itemKey]; } } set { lock(this.syncObject) { TItemKey itemKey = base.Keys[itemKeyIndex]; base[itemKey] = value; } } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ThreadSafeSortedList() /// <summary> /// 생성자 /// </summary> public ThreadSafeSortedList() { this.syncObject = new Object(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 항목 키 포함 여부 구하기 - ContainsItemKey(itemKey) /// <summary> /// 항목 키 포함 여부 구하기 /// </summary> /// <param name="itemKey">항목 키</param> /// <returns>항목 키 포함 여부</returns> public bool ContainsItemKey(TItemKey itemKey) { lock(this.syncObject) { return base.ContainsKey(itemKey); } } #endregion #region 항목 추가하기 - AddItem(itemKey, item) /// <summary> /// 항목 추가하기 /// </summary> /// <param name="itemKey">항목 키</param> /// <param name="item">항목</param> /// <returns>처리 결과</returns> public bool AddItem(TItemKey itemKey, TItem item) { lock(this.syncObject) { if(base.ContainsKey(itemKey)) { return false; } else { base.Add(itemKey, item); return true; } } } #endregion #region 항목 제거하기 - RemoveItemAt(index) /// <summary> /// 항목 제거하기 /// </summary> /// <param name="index">인덱스</param> public void RemoveItemAt(int index) { lock(this.syncObject) { RemoveAt(index); } } #endregion #region 항목 제거하기 - RemoveItem(itemKey) /// <summary> /// 항목 제거하기 /// </summary> /// <param name="itemKey">항목 키</param> /// <returns>처리 결과</returns> public bool RemoveItem(TItemKey itemKey) { lock(this.syncObject) { return base.Remove(itemKey); } } #endregion #region 데이터 제거하기 - ClearData() /// <summary> /// 데이터 제거하기 /// </summary> public void ClearData() { lock(this.syncObject) { Clear(); } } #endregion } } |
▶ ThreadSafeSortedListQueue.cs
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 |
namespace TestProject { /// <summary> /// 스레드 안전 정렬 리스트 큐 /// </summary> /// <typeparam name="TItemKey">항목 키 타입</typeparam> /// <typeparam name="TItem">항목 타입</typeparam> public class ThreadSafeSortedListQueue<TItemKey, TItem> : ThreadSafeSortedList<TItemKey, TItem> { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 항목 넣기 - EnqueueItem(itemKey, item) /// <summary> /// 항목 넣기 /// </summary> /// <param name="itemKey">항목 키</param> /// <param name="item">항목</param> /// <returns>처리 결과</returns> public bool EnqueueItem(TItemKey itemKey, TItem item) { return AddItem(itemKey, item); } #endregion #region 항목 엿보기 - PeekItem() /// <summary> /// 항목 엿보기 /// </summary> /// <returns>항목</returns> public TItem PeekItem() { lock(this.syncObject) { if(base.Count > 0) { return this[0]; } else { return default(TItem); } } } #endregion #region 항목 꺼내기 - DequeueItem() /// <summary> /// 항목 꺼내기 /// </summary> /// <returns>항목</returns> public TItem DequeueItem() { lock(this.syncObject) { if(Count > 0) { TItem item = this[0]; base.RemoveAt(0); return item; } else { return default(TItem); } } } #endregion } } |
▶ IKeyMessage.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
namespace TestProject { /// <summary> /// 키 메시지 인터페이스 /// </summary> public interface IKeyMessage<TItemKey> { //////////////////////////////////////////////////////////////////////////////////////////////////// Property #region 키 - Key /// <summary> /// 키 /// </summary> TItemKey Key { get; set; } #endregion } } |
▶ IPriorityMessage.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
namespace TestProject { /// <summary> /// 우선 순위 메시지 인터페이스 /// </summary> public interface IPriorityMessage { //////////////////////////////////////////////////////////////////////////////////////////////////// Property #region 우선 순위 - Priority /// <summary> /// 우선 순위 /// </summary> int Priority { get; set; } #endregion } } |
▶ ThreadSafePriorityQueue.cs
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
using System.Collections.Generic; namespace TestProject { /// <summary> /// 스레드 안전 우선 순위 큐 /// </summary> /// <typeparam name="TItemKey">항목 키 타입</typeparam> /// <typeparam name="TIem">항목 타입</typeparam> public class ThreadSafePriorityQueue<TItemKey, TItem> : Dictionary<int, ThreadSafeSortedListQueue<TItemKey, TItem>> where TItem : IKeyMessage<TItemKey>, IPriorityMessage { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Protected #region Field /// <summary> /// 동기 객체 /// </summary> protected object syncObject; /// <summary> /// 우선 순위 카운트 /// </summary> protected int priorityCount; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 우선 순위 카운트 - PriorityCount /// <summary> /// 우선 순위 카운트 /// </summary> public int PriorityCount { get { return this.priorityCount; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ThreadSafePriorityQueue(priorityCount) /// <summary> /// 생성자 /// </summary> /// <param name="priorityCount">우선 순위 카운트</param> public ThreadSafePriorityQueue(int priorityCount) { this.syncObject = new object(); this.priorityCount = priorityCount; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 항목 넣기 - EnqueueItem(item) /// <summary> /// 항목 넣기 /// </summary> /// <param name="item">항목</param> public void EnqueueItem(TItem item) { lock(this.syncObject) { int priority = item.Priority; if(priority < 0) { priority = 0; } if(priority >= this.priorityCount) { priority = this.priorityCount - 1; } ThreadSafeSortedListQueue<TItemKey, TItem> queue; if(base.ContainsKey(priority)) { queue = this[priority]; } else { queue = new ThreadSafeSortedListQueue<TItemKey, TItem>(); base.Add(priority, queue); } queue.EnqueueItem(item.Key, item); } } #endregion #region 항목 꺼내기 - DequeueItem() /// <summary> /// 항목 꺼내기 /// </summary> /// <returns>항목</returns> public TItem DequeueItem() { lock(this.syncObject) { for(int i = this.priorityCount - 1; i >= 0; i--) { if(ContainsKey(i)) { ThreadSafeSortedListQueue<TItemKey, TItem> queue = this[i]; if(queue.Count > 0) { TItem item = queue.DequeueItem(); return item; } } } return default(TItem); } } #endregion #region 항목 카운트 구하기 - GetItemCount() /// <summary> /// 항목 카운트 구하기 /// </summary> /// <returns>항목 카운트</returns> public int GetItemCount() { lock(this.syncObject) { int count = 0; foreach(KeyValuePair<int, ThreadSafeSortedListQueue<TItemKey, TItem>> keyValuePair in this) { count += keyValuePair.Value.Count; } return count; } } #endregion #region 데이터 지우기 - ClearData() /// <summary> /// 데이터 지우기 /// </summary> public void ClearData() { lock(this.syncObject) { foreach(KeyValuePair<int, ThreadSafeSortedListQueue<TItemKey, TItem>> keyValuePair in this) { ThreadSafeSortedListQueue<TItemKey, TItem> queue = keyValuePair.Value; queue.ClearData(); } Clear(); } } #endregion } } |
▶ TestMessage.cs
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 |
namespace TestProject { /// <summary> /// 테스트 메시지 /// </summary> public class TestMessage : IKeyMessage<string>, IPriorityMessage { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 키 - Key /// <summary> /// 키 /// </summary> public string Key { get; set; } #endregion #region 우선 순위 - Priority /// <summary> /// 우선 순위 /// </summary> public int Priority { get; set; } #endregion } } |
▶ Program.cs
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 |
using System; using System.Collections.Generic; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { List<TestMessage> sourceMessageList = new List<TestMessage>(); sourceMessageList.Add(new TestMessage { Priority = 0, Key = "01" }); sourceMessageList.Add(new TestMessage { Priority = 1, Key = "02" }); sourceMessageList.Add(new TestMessage { Priority = 2, Key = "03" }); sourceMessageList.Add(new TestMessage { Priority = 2, Key = "04" }); sourceMessageList.Add(new TestMessage { Priority = 3, Key = "05" }); sourceMessageList.Add(new TestMessage { Priority = 4, Key = "06" }); sourceMessageList.Add(new TestMessage { Priority = 4, Key = "07" }); ThreadSafePriorityQueue<string, TestMessage> messageQueue = new ThreadSafePriorityQueue<string, TestMessage>(5); foreach(TestMessage sourceMessage in sourceMessageList) { messageQueue.EnqueueItem(sourceMessage); Console.WriteLine("ENQUEUE ITEM : {0}, {1}", sourceMessage.Priority, sourceMessage.Key); } TestMessage message; do { message = messageQueue.DequeueItem(); if(message == null) { return; } Console.WriteLine("DEQUEUE ITEM : {0}, {1}", message.Priority, message.Key); } while(true); } #endregion } } |