Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. zad 2
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Collections.Concurrent;
  12.  
  13. namespace zad2,3
  14. {
  15. class Program
  16. {
  17.  
  18. static void Main(string[] args)
  19. {
  20. Stopwatch zegarek = new Stopwatch();
  21. List<Thread> watki = new List<Thread>();
  22. ConcurrentBag<int> liczby = new ConcurrentBag<int>();
  23. Random rand = new Random();
  24. for (int i = 0; i < 100; i++)
  25. {
  26. var watek = new Thread(() =>
  27. {
  28. for (int l = 0; l < 10000; l++)
  29. liczby.Add(rand.Next());
  30. });
  31. watki.Add(watek);
  32. zegarek.Start();
  33. watek.Start();
  34. }
  35. foreach (var watek in watki)
  36. {
  37. watek.Join();
  38.  
  39. }
  40. zegarek.Stop();
  41. Console.WriteLine("Liczba elementów w liście zwykłej: {0}", liczby.Count);
  42. Console.WriteLine("Upłynęło {0} ms", zegarek.ElapsedMilliseconds);
  43. Console.ReadLine();
  44. }
  45. }
  46. }
  47.  
  48. zad4
  49. using System;
  50. using System.Collections.Generic;
  51. using System.Collections;
  52. using System.Diagnostics;
  53. using System.Linq;
  54. using System.Text;
  55. using System.Threading;
  56. using System.Threading.Tasks;
  57.  
  58. namespace zad2
  59. {
  60. class Program
  61. {
  62. static object block = new object();
  63. static void Main(string[] args)
  64. {
  65. Stopwatch zegarek = new Stopwatch();
  66. List<Thread> watki = new List<Thread>();
  67. List<int> liczby = new List<int>(1000000);
  68. Random rand = new Random();
  69. for (int i = 0; i < 100; i++)
  70. {
  71. var watek = new Thread(() =>
  72. {
  73. lock (block)
  74. {
  75. for (int l = 0; l < 10000; l++)
  76. liczby.Add(rand.Next());
  77. }
  78. });
  79.  
  80. watki.Add(watek);
  81. zegarek.Start();
  82. watek.Start();
  83. }
  84. foreach (var watek in watki)
  85. {
  86. watek.Join();
  87.  
  88. }
  89. zegarek.Stop();
  90. Console.WriteLine("Liczba elementów w liście zwykłej: {0}", liczby.Count);
  91. Console.WriteLine("Upłynęło {0} ms", zegarek.ElapsedMilliseconds);
  92. Console.ReadLine();
  93. }
  94. }
  95. }
  96.  
  97. zad 5
  98.  
  99. using System;
  100. using System.Collections.Concurrent;
  101. using System.Collections.Generic;
  102. using System.Linq;
  103. using System.Text;
  104. using System.Threading.Tasks;
  105. using System.Threading;
  106. using System.Diagnostics;
  107.  
  108. namespace KolekcjaUnikalnaLock
  109. {
  110. public static class Extensions
  111. {
  112.  
  113. public static bool IsUnique(this List<Thread> lista)
  114. {
  115. if (lista.Distinct().Count() == lista.Count()) //jeżeli liczba unikalnych jest równa liczbie wszystkich to wszystkie są unikalne.
  116. {
  117. return true;
  118. }
  119. // Console.WriteLine($"Liczba disctinct: {lista.Distinct().Count()} jest różna od liczby elementów: {lista.Count()}");
  120. return false;
  121. }
  122.  
  123. public static bool IsUnique(this ConcurrentBag<int> cb)
  124. {
  125. if (cb.Distinct().Count() == cb.Count()) return true;
  126. else return false;
  127. }
  128. static object block = new object();
  129. static void Main(string[] args)
  130. {
  131. List<Thread> watki = new List<Thread>();
  132. ConcurrentBag<int>liczby = new ConcurrentBag<int>();
  133. int liczba=0;
  134. Stopwatch sw = new Stopwatch();
  135. sw.Start();
  136. Random rand = new Random();
  137. for (int i = 0; i < 100; i++)
  138. {
  139. var watek = new Thread(() =>
  140. {
  141.  
  142. for (int l = 0; l < 100; l++)
  143. lock (block)
  144. {
  145. liczba = rand.Next(10001);
  146. }
  147.  
  148. lock (block)
  149. {
  150. while (liczby.Any(x => x == liczba))
  151. {
  152.  
  153. liczba = rand.Next(10001);
  154.  
  155. };
  156. }
  157. liczby.Add(liczba);
  158.  
  159. });
  160.  
  161. watki.Add(watek);
  162.  
  163. watek.Start();
  164. }
  165. foreach (var watek in watki)
  166. {
  167.  
  168. watek.Join();
  169.  
  170. }
  171. sw.Stop();
  172.  
  173. Console.WriteLine("Upłynęło {0} ms",sw.ElapsedMilliseconds);
  174.  
  175. if (liczby.IsUnique()) //zastosowanie metody rozszerzającej dla CB
  176. Console.WriteLine("wszystkie liczby są unikalne");
  177. else
  178. Console.WriteLine("Nie wszystkie liczby są unikalne");
  179.  
  180.  
  181.  
  182. Console.ReadLine();
  183. }
  184. }
  185. }
  186.  
  187.  
  188. zad 6
  189. using System;
  190. using System.Collections.Concurrent;
  191. using System.Collections.Generic;
  192. using System.Linq;
  193. using System.Text;
  194. using System.Threading.Tasks;
  195. using System.Threading;
  196. using System.Diagnostics;
  197.  
  198. namespace KolekcjaDictionary
  199. {
  200. public static class Extensions
  201. {
  202.  
  203. public static bool IsUnique(this List<Thread> lista)
  204. {
  205. if (lista.Distinct().Count() == lista.Count()) //jeżeli liczba unikalnych jest równa liczbie wszystkich to wszystkie są unikalne.
  206. {
  207. return true;
  208. }
  209. // Console.WriteLine($"Liczba disctinct: {lista.Distinct().Count()} jest różna od liczby elementów: {lista.Count()}");
  210. return false;
  211. }
  212.  
  213. public static bool IsUnique(this ConcurrentDictionary<int, object> cd)
  214. {
  215. if (cd.Distinct().Count() == cd.Count()) return true;
  216. else return false;
  217. }
  218. static object blokowacz = new object();
  219. static void Main(string[] args)
  220. {
  221. List<Thread> watki = new List<Thread>();
  222. //ConcurrentBag<int> liczby = new ConcurrentBag<int>();
  223. var liczby = new ConcurrentDictionary<int, object>();
  224. int liczba = 0;
  225. Stopwatch zegarek = new Stopwatch();
  226. zegarek.Start();
  227. Random rand = new Random();
  228. for (int i = 0; i < 100; i++)
  229. {
  230. var watek = new Thread(() =>
  231. {
  232.  
  233. for (int l = 0; l < 100; l++)
  234. lock (blokowacz)
  235. {
  236. liczba = rand.Next(10001);
  237. }
  238. lock (blokowacz)
  239. {
  240. //while (liczby.Any(x => x == rand.Next(10001)))
  241. //{
  242.  
  243. liczba = rand.Next(10001);
  244.  
  245. //};
  246. }
  247. liczby.TryAdd(liczba,null);
  248.  
  249. });
  250.  
  251. watki.Add(watek);
  252.  
  253. watek.Start();
  254. }
  255. foreach (var watek in watki)
  256. {
  257.  
  258. watek.Join();
  259.  
  260. }
  261. zegarek.Stop();
  262.  
  263. Console.WriteLine("Upłynęło {0} ms", zegarek.ElapsedMilliseconds);
  264.  
  265. if (liczby.IsUnique()) //zastosowanie metody rozszerzającej dla CB
  266. Console.WriteLine("wszystkie liczby są unikalne");
  267. else
  268. Console.WriteLine("Nie wszystkie liczby są unikalne");
  269.  
  270.  
  271.  
  272. Console.ReadLine();
  273. }
  274. }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement