Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.95 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.  
  98. zad 5
  99.  
  100. using System;
  101. using System.Collections.Concurrent;
  102. using System.Collections.Generic;
  103. using System.Linq;
  104. using System.Text;
  105. using System.Threading.Tasks;
  106. using System.Threading;
  107. using System.Diagnostics;
  108.  
  109. namespace KolekcjaUnikalnaLock
  110. {
  111. public static class Extensions
  112. {
  113.  
  114. public static bool IsUnique(this List<Thread> lista)
  115. {
  116. if (lista.Distinct().Count() == lista.Count()) //jeżeli liczba unikalnych jest równa liczbie wszystkich to wszystkie są unikalne.
  117. {
  118. return true;
  119. }
  120. // Console.WriteLine($"Liczba disctinct: {lista.Distinct().Count()} jest różna od liczby elementów: {lista.Count()}");
  121. return false;
  122. }
  123.  
  124. public static bool IsUnique(this ConcurrentBag<int> cb)
  125. {
  126. if (cb.Distinct().Count() == cb.Count()) return true;
  127. else return false;
  128. }
  129. static object block = new object();
  130. static void Main(string[] args)
  131. {
  132. List<Thread> watki = new List<Thread>();
  133. ConcurrentBag<int>liczby = new ConcurrentBag<int>();
  134. int liczba=0;
  135. Stopwatch sw = new Stopwatch();
  136. sw.Start();
  137. Random rand = new Random();
  138. for (int i = 0; i < 100; i++)
  139. {
  140. var watek = new Thread(() =>
  141. {
  142.  
  143. for (int l = 0; l < 100; l++)
  144. lock (block)
  145. {
  146. liczba = rand.Next(10001);
  147. }
  148.  
  149. lock (block)
  150. {
  151. while (liczby.Any(x => x == liczba))
  152. {
  153.  
  154. liczba = rand.Next(10001);
  155.  
  156. };
  157. }
  158. liczby.Add(liczba);
  159.  
  160. });
  161.  
  162. watki.Add(watek);
  163.  
  164. watek.Start();
  165. }
  166. foreach (var watek in watki)
  167. {
  168.  
  169. watek.Join();
  170.  
  171. }
  172. sw.Stop();
  173.  
  174. Console.WriteLine("Upłynęło {0} ms",sw.ElapsedMilliseconds);
  175.  
  176. if (liczby.IsUnique()) //zastosowanie metody rozszerzającej dla CB
  177. Console.WriteLine("wszystkie liczby są unikalne");
  178. else
  179. Console.WriteLine("Nie wszystkie liczby są unikalne");
  180.  
  181.  
  182.  
  183. Console.ReadLine();
  184. }
  185. }
  186. }
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195. zad 6
  196. using System;
  197. using System.Collections.Concurrent;
  198. using System.Collections.Generic;
  199. using System.Linq;
  200. using System.Text;
  201. using System.Threading.Tasks;
  202. using System.Threading;
  203. using System.Diagnostics;
  204.  
  205. namespace KolekcjaDictionary
  206. {
  207. public static class Extensions
  208. {
  209.  
  210. public static bool IsUnique(this List<Thread> lista)
  211. {
  212. if (lista.Distinct().Count() == lista.Count()) //jeżeli liczba unikalnych jest równa liczbie wszystkich to wszystkie są unikalne.
  213. {
  214. return true;
  215. }
  216. // Console.WriteLine($"Liczba disctinct: {lista.Distinct().Count()} jest różna od liczby elementów: {lista.Count()}");
  217. return false;
  218. }
  219.  
  220. public static bool IsUnique(this ConcurrentDictionary<int, object> cd)
  221. {
  222. if (cd.Distinct().Count() == cd.Count()) return true;
  223. else return false;
  224. }
  225. static object blokowacz = new object();
  226. static void Main(string[] args)
  227. {
  228. List<Thread> watki = new List<Thread>();
  229. //ConcurrentBag<int> liczby = new ConcurrentBag<int>();
  230. var liczby = new ConcurrentDictionary<int, object>();
  231. int liczba = 0;
  232. Stopwatch zegarek = new Stopwatch();
  233. zegarek.Start();
  234. Random rand = new Random();
  235. for (int i = 0; i < 100; i++)
  236. {
  237. var watek = new Thread(() =>
  238. {
  239.  
  240. for (int l = 0; l < 100; l++)
  241. lock (blokowacz)
  242. {
  243. liczba = rand.Next(10001);
  244. }
  245. lock (blokowacz)
  246. {
  247. //while (liczby.Any(x => x == rand.Next(10001)))
  248. //{
  249.  
  250. liczba = rand.Next(10001);
  251.  
  252. //};
  253. }
  254. liczby.TryAdd(liczba,null);
  255.  
  256. });
  257.  
  258. watki.Add(watek);
  259.  
  260. watek.Start();
  261. }
  262. foreach (var watek in watki)
  263. {
  264.  
  265. watek.Join();
  266.  
  267. }
  268. zegarek.Stop();
  269.  
  270. Console.WriteLine("Upłynęło {0} ms", zegarek.ElapsedMilliseconds);
  271.  
  272. if (liczby.IsUnique()) //zastosowanie metody rozszerzającej dla CB
  273. Console.WriteLine("wszystkie liczby są unikalne");
  274. else
  275. Console.WriteLine("Nie wszystkie liczby są unikalne");
  276.  
  277.  
  278.  
  279. Console.ReadLine();
  280. }
  281. }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement