Guest User

Untitled

a guest
Jul 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. Type: Type1 Count: 38
  2. Type: Type2 Count: 31
  3. Type: Type3 Count: 31
  4. Total for all types: 100
  5.  
  6. /* Incorrect version, not using ConcurrentQueue, showing incorrect results */
  7.  
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Threading.Tasks;
  12.  
  13. namespace ConcurrentQueue
  14. {
  15. class Program
  16. {
  17. private static readonly int OrderCount = 100;
  18.  
  19. private static IEnumerable<Order> _orders;
  20.  
  21. private static Dictionary<OrderTypeEnum, Aggregate> _aggregates;
  22.  
  23. static void Main(string[] args)
  24. {
  25. //Prepare
  26. InitializeAggregates();
  27. _orders = CreateOrders();
  28.  
  29. //Execute
  30. MainAsync(args).GetAwaiter().GetResult();
  31. }
  32.  
  33. static async Task MainAsync(string[] args)
  34. {
  35. await Task.Run(() => ProcessOrders());
  36.  
  37. ShowOutput();
  38. }
  39.  
  40. public static void ProcessOrders()
  41. {
  42. var aggregator = new Aggregator();
  43. Parallel.ForEach(_orders, order => {
  44. aggregator.Aggregate(order, _aggregates);
  45. });
  46. }
  47.  
  48. private static IEnumerable<Order> CreateOrders()
  49. {
  50. var orderList = new Collection<Order>();
  51.  
  52. for (var i = 1; i <= OrderCount; i++)
  53. {
  54. var order = CreateOrder(i);
  55. orderList.Add(order);
  56. }
  57.  
  58. return orderList;
  59. }
  60.  
  61. private static void InitializeAggregates()
  62. {
  63. _aggregates = new Dictionary<OrderTypeEnum, Aggregate>();
  64. _aggregates[OrderTypeEnum.Type1] = new Aggregate();
  65. _aggregates[OrderTypeEnum.Type2] = new Aggregate();
  66. _aggregates[OrderTypeEnum.Type3] = new Aggregate();
  67. }
  68.  
  69. private static Order CreateOrder(int id)
  70. {
  71. var order = new Order() { Id = id, OrderType = GetRandomAggregtationType() };
  72. return order;
  73. }
  74.  
  75. private static OrderTypeEnum GetRandomAggregtationType()
  76. {
  77. Array values = Enum.GetValues(typeof(OrderTypeEnum));
  78. var random = new Random();
  79. return (OrderTypeEnum)values.GetValue(random.Next(values.Length));
  80. }
  81.  
  82. private static void ShowOutput()
  83. {
  84. Console.WriteLine($"Type: {OrderTypeEnum.Type1} Count: {_aggregates[OrderTypeEnum.Type1].Count}");
  85. Console.WriteLine($"Type: {OrderTypeEnum.Type2} Count: {_aggregates[OrderTypeEnum.Type2].Count}");
  86. Console.WriteLine($"Type: {OrderTypeEnum.Type3} Count: {_aggregates[OrderTypeEnum.Type3].Count}");
  87. var total =
  88. _aggregates[OrderTypeEnum.Type1].Count +
  89. _aggregates[OrderTypeEnum.Type2].Count +
  90. _aggregates[OrderTypeEnum.Type3].Count;
  91. Console.WriteLine($"Total for all types: {total}");
  92. Console.ReadKey();
  93. }
  94. }
  95.  
  96. public class Order
  97. {
  98. public int Id { get; set; }
  99. public OrderTypeEnum OrderType { get; set; }
  100. }
  101.  
  102. public class Aggregator
  103. {
  104. public void Aggregate(Order order, Dictionary<OrderTypeEnum, Aggregate> aggregates)
  105. {
  106. aggregates[order.OrderType].Count++;
  107. }
  108. }
  109.  
  110. public class Aggregate
  111. {
  112. public int Count { get; set; }
  113. }
  114.  
  115. public enum OrderTypeEnum
  116. {
  117. Type1 = 1,
  118. Type2 = 2,
  119. Type3 = 3
  120. }
  121. }
  122.  
  123. /* improved version using ConcurrentQueue, showing correct results */
  124.  
  125. using System;
  126. using System.Collections.Concurrent;
  127. using System.Collections.Generic;
  128. using System.Collections.ObjectModel;
  129. using System.Threading.Tasks;
  130.  
  131. namespace ConcurrentQueue
  132. {
  133. class Program
  134. {
  135. private static readonly int OrderCount = 100;
  136.  
  137. private static IEnumerable<Order> _orders;
  138.  
  139. private static Dictionary<OrderTypeEnum, Aggregate> _aggregates;
  140.  
  141. static void Main(string[] args)
  142. {
  143. //Prepare
  144. InitializeAggregates();
  145. _orders = CreateOrders();
  146.  
  147. //Execute
  148. var proxy = new OrderProxy();
  149. var ordersQueue = new ConcurrentQueue<OrderResult>();
  150. Parallel.ForEach(_orders, order => {
  151. var orderResult = proxy.PlaceOrder(order);
  152. ordersQueue.Enqueue(orderResult);
  153. });
  154.  
  155. foreach (var order in ordersQueue)
  156. {
  157. _aggregates[order.OrderType].Count++;
  158. }
  159.  
  160. ShowOutput();
  161. }
  162.  
  163. private static IEnumerable<Order> CreateOrders()
  164. {
  165. var orderList = new Collection<Order>();
  166.  
  167. for (var i = 1; i <= OrderCount; i++)
  168. {
  169. var order = CreateOrder(i);
  170. orderList.Add(order);
  171. }
  172.  
  173. return orderList;
  174. }
  175.  
  176. private static void InitializeAggregates()
  177. {
  178. _aggregates = new Dictionary<OrderTypeEnum, Aggregate>();
  179. _aggregates[OrderTypeEnum.Type1] = new Aggregate();
  180. _aggregates[OrderTypeEnum.Type2] = new Aggregate();
  181. _aggregates[OrderTypeEnum.Type3] = new Aggregate();
  182. }
  183.  
  184. private static Order CreateOrder(int id)
  185. {
  186. var order = new Order() { Id = id, AggregateType = GetRandomAggregtationType() };
  187. return order;
  188. }
  189.  
  190. private static OrderTypeEnum GetRandomAggregtationType()
  191. {
  192. Array values = Enum.GetValues(typeof(OrderTypeEnum));
  193. var random = new Random();
  194. return (OrderTypeEnum)values.GetValue(random.Next(values.Length));
  195. }
  196.  
  197. private static void ShowOutput()
  198. {
  199. Console.WriteLine($"Type: {OrderTypeEnum.Type1} Count: {_aggregates[OrderTypeEnum.Type1].Count}");
  200. Console.WriteLine($"Type: {OrderTypeEnum.Type2} Count: {_aggregates[OrderTypeEnum.Type2].Count}");
  201. Console.WriteLine($"Type: {OrderTypeEnum.Type3} Count: {_aggregates[OrderTypeEnum.Type3].Count}");
  202. var total =
  203. _aggregates[OrderTypeEnum.Type1].Count +
  204. _aggregates[OrderTypeEnum.Type2].Count +
  205. _aggregates[OrderTypeEnum.Type3].Count;
  206. Console.WriteLine($"Total for all types: {total}");
  207. Console.ReadKey();
  208. }
  209. }
  210.  
  211. public class Order
  212. {
  213. public int Id { get; set; }
  214. public OrderTypeEnum AggregateType { get; set; }
  215. }
  216.  
  217. public class OrderResult
  218. {
  219. public int Id { get; set; }
  220. public OrderTypeEnum OrderType { get; set; }
  221. }
  222.  
  223. public class OrderProxy
  224. {
  225. public OrderResult PlaceOrder(Order order)
  226. {
  227. var orderResult = new OrderResult() { Id = order.Id, OrderType = order.AggregateType };
  228. return orderResult;
  229. }
  230. }
  231.  
  232. public class Aggregate
  233. {
  234. public OrderTypeEnum OrderType { get; set; }
  235. public int Count { get; set; }
  236. }
  237.  
  238. public enum OrderTypeEnum
  239. {
  240. Type1 = 1,
  241. Type2 = 2,
  242. Type3 = 3
  243. }
  244. }
  245.  
  246. public class Aggregator
  247. {
  248. public void Aggregate(Order order, Dictionary<OrderTypeEnum, Aggregate> aggregates)
  249. {
  250. Interlocked.Increment(ref aggregates[order.OrderType].Count);
  251. }
  252. }
Add Comment
Please, Sign In to add comment