Advertisement
kikosiak

Untitled

Dec 16th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.46 KB | None | 0 0
  1. -------------------zad 2------------------
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Diagnostics;
  10.  
  11. namespace NiebezpiecznaKolekcja
  12. {
  13. class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. List<Thread> watki = new List<Thread>();
  18. List<int> liczby = new List<int>(1000000);
  19. Random rand = new Random();
  20. Stopwatch stopWatch = new Stopwatch();
  21. stopWatch.Start();
  22. for (int i = 0; i < 100; i++)
  23. {
  24. var watek = new Thread(() =>
  25. {
  26. for (int l = 0; l < 10000; l++)
  27. liczby.Add(rand.Next());
  28. });
  29. watki.Add(watek);
  30. watek.Start();
  31. }
  32. stopWatch.Stop();
  33. // Get the elapsed time as a TimeSpan value.
  34. TimeSpan ts = stopWatch.Elapsed;
  35.  
  36. // Format and display the TimeSpan value.
  37. string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
  38. ts.Hours, ts.Minutes, ts.Seconds,
  39. ts.Milliseconds / 10);
  40. Console.WriteLine("RunTime " + elapsedTime);
  41. foreach (var watek in watki)
  42. {
  43. watek.Join();
  44. }
  45. Console.WriteLine($"Liczba elementów w liście zwykłej: {liczby.Count}");
  46. Console.ReadLine();
  47. }
  48. }
  49. }
  50.  
  51. ----------------------zad 3---------------------------
  52. using System;
  53. using System.Collections.Generic;
  54. using System.Linq;
  55. using System.Text;
  56. using System.Threading;
  57. using System.Threading.Tasks;
  58. using System.Diagnostics;
  59. using System.Collections.Concurrent;
  60.  
  61. namespace NiebezpiecznaKolekcja
  62. {
  63. class Program
  64. {
  65. static void Main(string[] args)
  66. {
  67. ConcurrentBag<Thread> watki = new ConcurrentBag<Thread>();
  68. ConcurrentBag<int> liczby = new ConcurrentBag<int>();
  69. Random rand = new Random();
  70. Stopwatch stopWatch = new Stopwatch();
  71. stopWatch.Start();
  72. for (int i = 0; i < 100; i++)
  73. {
  74. var watek = new Thread(() =>
  75. {
  76. for (int l = 0; l < 10000; l++)
  77. liczby.Add(rand.Next());
  78. });
  79. watki.Add(watek);
  80. watek.Start();
  81. }
  82. stopWatch.Stop();
  83. // Get the elapsed time as a TimeSpan value.
  84. TimeSpan ts = stopWatch.Elapsed;
  85.  
  86. // Format and display the TimeSpan value.
  87. string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
  88. ts.Hours, ts.Minutes, ts.Seconds,
  89. ts.Milliseconds / 10);
  90. Console.WriteLine("RunTime " + elapsedTime);
  91. foreach (var watek in watki)
  92. {
  93. watek.Join();
  94. }
  95. Console.WriteLine($"Liczba elementów w liście zwykłej: {liczby.Count}");
  96. Console.ReadLine();
  97. }
  98. }
  99. }
  100.  
  101. --------------------------zad 4---------------------
  102.  
  103. using System;
  104. using System.Collections.Generic;
  105. using System.Linq;
  106. using System.Text;
  107. using System.Threading;
  108. using System.Threading.Tasks;
  109. using System.Diagnostics;
  110.  
  111. namespace NiebezpiecznaKolekcja
  112. {
  113. class Program
  114. {
  115. static void Main(string[] args)
  116. {
  117. object _Lock = new object();
  118. List<Thread> watki = new List<Thread>();
  119. List<int> liczby = new List<int>(1000000);
  120. Random rand = new Random();
  121. Stopwatch stopWatch = new Stopwatch();
  122. stopWatch.Start();
  123. for (int i = 0; i < 100; i++)
  124. {
  125. var watek = new Thread(() =>
  126. {
  127. for (int l = 0; l < 10000; l++){
  128. lock(_Lock){
  129. liczby.Add(rand.Next());
  130. }
  131. }
  132. });
  133. watki.Add(watek);
  134. watek.Start();
  135. }
  136. stopWatch.Stop();
  137. // Get the elapsed time as a TimeSpan value.
  138. TimeSpan ts = stopWatch.Elapsed;
  139.  
  140. // Format and display the TimeSpan value.
  141. string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
  142. ts.Hours, ts.Minutes, ts.Seconds,
  143. ts.Milliseconds / 10);
  144. Console.WriteLine("RunTime " + elapsedTime);
  145. foreach (var watek in watki)
  146. {
  147. watek.Join();
  148. }
  149. Console.WriteLine($"Liczba elementów w liście zwykłej: {liczby.Count}");
  150. Console.ReadLine();
  151. }
  152. }
  153. }
  154.  
  155. ------------------------zad 5----------------------------
  156.  
  157. using System;
  158. using System.Collections.Concurrent;
  159. using System.Collections.Generic;
  160. using System.Linq;
  161. using System.Text;
  162. using System.Threading.Tasks;
  163. using System.Threading;
  164. using System.Diagnostics;
  165.  
  166. namespace KolekcjaUnikalnaLock
  167. {
  168. class Program
  169. {
  170. static void Main(string[] args)
  171. {
  172. object _Lock = new object();
  173. List<Thread> watki = new List<Thread>();
  174. ConcurrentBag<int> liczby = new ConcurrentBag<int>();
  175. Stopwatch sw = new Stopwatch();
  176. sw.Start();
  177. Random rand = new Random();
  178. for (int i = 0; i < 100; i++)
  179. {
  180. var watek = new Thread(() =>
  181. {
  182.  
  183. for (int l = 0; l < 100; l++)
  184. {
  185. int liczba = rand.Next(10001);
  186. lock(_Lock){
  187. while (liczby.Any(x => x == liczba))
  188. {
  189. liczba = rand.Next(10001);
  190. };
  191.  
  192. liczby.Add(liczba);
  193. }
  194. }
  195. });
  196. watki.Add(watek);
  197. watek.Start();
  198. }
  199. foreach (var watek in watki)
  200. {
  201. watek.Join();
  202. }
  203. sw.Stop();
  204.  
  205. Console.WriteLine($"Upłynęło {sw.ElapsedMilliseconds} ms");
  206. if (liczby.IsUnique()) //zastosowanie metody rozszerzającej dla CB
  207. Console.WriteLine("wszystkie liczby są unikalne");
  208. else
  209. Console.WriteLine("Nie wszystkie liczby są unikalne");
  210.  
  211.  
  212. Console.ReadLine();
  213. }
  214. }
  215. }
  216.  
  217. ----------------------- zad 6--------------------
  218.  
  219. using System;
  220. using System.Collections.Concurrent;
  221. using System.Collections.Generic;
  222. using System.Linq;
  223. using System.Text;
  224. using System.Threading.Tasks;
  225. using System.Threading;
  226.  
  227. namespace KolekcjaUnikalnaLock
  228. {
  229. public static class Extensions
  230. {
  231. public static bool IsUnique(this List<int> lista)
  232. {
  233. if (lista.Distinct().Count() == lista.Count()) //jeżeli liczba unikalnych jest równa liczbie wszystkich to wszystkie są unikalne.
  234. {
  235. return true;
  236. }
  237. // Console.WriteLine($"Liczba disctinct: {lista.Distinct().Count()} jest różna od liczby elementów: {lista.Count()}");
  238. return false;
  239. }
  240.  
  241. public static bool IsUnique(this ConcurrentBag<int> cb)
  242. {
  243. if (cb.Distinct().Count() == cb.Count()) return true;
  244. else return false;
  245. }
  246.  
  247. public static bool IsUnique(this ConcurrentDictionary<int,int> cb)
  248. {
  249. if (cb.Distinct().Count() == cb.Count()) return true;
  250. else return false;
  251. }
  252. }
  253. }
  254.  
  255.  
  256. ---------------------
  257.  
  258. using System;
  259. using System.Collections.Concurrent;
  260. using System.Collections.Generic;
  261. using System.Linq;
  262. using System.Text;
  263. using System.Threading.Tasks;
  264. using System.Threading;
  265. using System.Diagnostics;
  266.  
  267. namespace KolekcjaUnikalnaLock
  268. {
  269. class Program
  270. {
  271. static void Main(string[] args)
  272. {
  273. object _Lock = new object();
  274. List<Thread> watki = new List<Thread>();
  275. ConcurrentDictionary <int,int> liczby = new ConcurrentDictionary <int,int>();
  276. Stopwatch sw = new Stopwatch();
  277. sw.Start();
  278. Random rand = new Random();
  279. for (int i = 0; i < 100; i++)
  280. {
  281. var watek = new Thread(() =>
  282. {
  283.  
  284. for (int l = 0; l < 100; l++)
  285. {
  286. int liczba = rand.Next(10001);
  287. lock(_Lock){
  288.  
  289.  
  290. liczby[l]=liczba;
  291. }
  292. }
  293. });
  294. watki.Add(watek);
  295. watek.Start();
  296. }
  297. foreach (var watek in watki)
  298. {
  299. watek.Join();
  300. }
  301. sw.Stop();
  302.  
  303. Console.WriteLine($"Upłynęło {sw.ElapsedMilliseconds} ms");
  304. if (liczby.IsUnique()) //zastosowanie metody rozszerzającej dla CB
  305. Console.WriteLine("wszystkie liczby są unikalne");
  306. else
  307. Console.WriteLine("Nie wszystkie liczby są unikalne");
  308.  
  309.  
  310. Console.ReadLine();
  311. }
  312. }
  313. }
  314.  
  315. -------------------------zad 7 -------------------------
  316.  
  317. using System;
  318. using System.Collections.Concurrent;
  319. using System.Collections.Generic;
  320. using System.Linq;
  321. using System.Text;
  322. using System.Threading.Tasks;
  323. using System.Threading;
  324. using System.Diagnostics;
  325.  
  326.  
  327. namespace queue
  328. {
  329. class Program
  330. {
  331. static void Main(string[] args)
  332. {
  333. var cq = new ConcurrentQueue <int>();
  334. List<Thread> watki = new List<Thread>();
  335. Random rand = new Random();
  336. //prodcent wkłada do kolekcji 100 elementów z losowymi odstępami do 500ms
  337. var producent = new Thread(() =>
  338. {
  339. for (int l = 0; l < 100; l++)
  340. {
  341. cq.Enqueue(l);
  342. Console.WriteLine($"Wkładam {l}");
  343. Thread.Sleep(rand.Next(500));
  344. }
  345.  
  346. });
  347. producent.Start();
  348.  
  349. for (int i = 0; i < 10; i++)
  350. {
  351. var watek = new Thread(() =>
  352. {
  353.  
  354. for (int l = 0; l < 10; l++)
  355. {
  356. try
  357. {
  358. int result2;
  359. bool udaloSie = false;
  360. while(udaloSie = cq.TryDequeue(out result2))
  361. Console.WriteLine($"Odbieram {result2} ");
  362.  
  363. }
  364. catch (Exception ex)
  365. {
  366. Console.WriteLine("Wyjątek przy odebraniu: ", ex.Message);
  367. }
  368.  
  369. Thread.Sleep(rand.Next(5000));
  370. }
  371. });
  372. watki.Add(watek);
  373. watek.Start();
  374. }
  375. foreach (var watek in watki)
  376. {
  377. watek.Join();
  378. }
  379. producent.Join();
  380. }
  381. }
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement