Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------zad 2------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Diagnostics;
- namespace NiebezpiecznaKolekcja
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Thread> watki = new List<Thread>();
- List<int> liczby = new List<int>(1000000);
- Random rand = new Random();
- Stopwatch stopWatch = new Stopwatch();
- stopWatch.Start();
- for (int i = 0; i < 100; i++)
- {
- var watek = new Thread(() =>
- {
- for (int l = 0; l < 10000; l++)
- liczby.Add(rand.Next());
- });
- watki.Add(watek);
- watek.Start();
- }
- stopWatch.Stop();
- // Get the elapsed time as a TimeSpan value.
- TimeSpan ts = stopWatch.Elapsed;
- // Format and display the TimeSpan value.
- string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
- ts.Hours, ts.Minutes, ts.Seconds,
- ts.Milliseconds / 10);
- Console.WriteLine("RunTime " + elapsedTime);
- foreach (var watek in watki)
- {
- watek.Join();
- }
- Console.WriteLine($"Liczba elementów w liście zwykłej: {liczby.Count}");
- Console.ReadLine();
- }
- }
- }
- ----------------------zad 3---------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Diagnostics;
- using System.Collections.Concurrent;
- namespace NiebezpiecznaKolekcja
- {
- class Program
- {
- static void Main(string[] args)
- {
- ConcurrentBag<Thread> watki = new ConcurrentBag<Thread>();
- ConcurrentBag<int> liczby = new ConcurrentBag<int>();
- Random rand = new Random();
- Stopwatch stopWatch = new Stopwatch();
- stopWatch.Start();
- for (int i = 0; i < 100; i++)
- {
- var watek = new Thread(() =>
- {
- for (int l = 0; l < 10000; l++)
- liczby.Add(rand.Next());
- });
- watki.Add(watek);
- watek.Start();
- }
- stopWatch.Stop();
- // Get the elapsed time as a TimeSpan value.
- TimeSpan ts = stopWatch.Elapsed;
- // Format and display the TimeSpan value.
- string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
- ts.Hours, ts.Minutes, ts.Seconds,
- ts.Milliseconds / 10);
- Console.WriteLine("RunTime " + elapsedTime);
- foreach (var watek in watki)
- {
- watek.Join();
- }
- Console.WriteLine($"Liczba elementów w liście zwykłej: {liczby.Count}");
- Console.ReadLine();
- }
- }
- }
- --------------------------zad 4---------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Diagnostics;
- namespace NiebezpiecznaKolekcja
- {
- class Program
- {
- static void Main(string[] args)
- {
- object _Lock = new object();
- List<Thread> watki = new List<Thread>();
- List<int> liczby = new List<int>(1000000);
- Random rand = new Random();
- Stopwatch stopWatch = new Stopwatch();
- stopWatch.Start();
- for (int i = 0; i < 100; i++)
- {
- var watek = new Thread(() =>
- {
- for (int l = 0; l < 10000; l++){
- lock(_Lock){
- liczby.Add(rand.Next());
- }
- }
- });
- watki.Add(watek);
- watek.Start();
- }
- stopWatch.Stop();
- // Get the elapsed time as a TimeSpan value.
- TimeSpan ts = stopWatch.Elapsed;
- // Format and display the TimeSpan value.
- string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
- ts.Hours, ts.Minutes, ts.Seconds,
- ts.Milliseconds / 10);
- Console.WriteLine("RunTime " + elapsedTime);
- foreach (var watek in watki)
- {
- watek.Join();
- }
- Console.WriteLine($"Liczba elementów w liście zwykłej: {liczby.Count}");
- Console.ReadLine();
- }
- }
- }
- ------------------------zad 5----------------------------
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- using System.Diagnostics;
- namespace KolekcjaUnikalnaLock
- {
- class Program
- {
- static void Main(string[] args)
- {
- object _Lock = new object();
- List<Thread> watki = new List<Thread>();
- ConcurrentBag<int> liczby = new ConcurrentBag<int>();
- Stopwatch sw = new Stopwatch();
- sw.Start();
- Random rand = new Random();
- for (int i = 0; i < 100; i++)
- {
- var watek = new Thread(() =>
- {
- for (int l = 0; l < 100; l++)
- {
- int liczba = rand.Next(10001);
- lock(_Lock){
- while (liczby.Any(x => x == liczba))
- {
- liczba = rand.Next(10001);
- };
- liczby.Add(liczba);
- }
- }
- });
- watki.Add(watek);
- watek.Start();
- }
- foreach (var watek in watki)
- {
- watek.Join();
- }
- sw.Stop();
- Console.WriteLine($"Upłynęło {sw.ElapsedMilliseconds} ms");
- if (liczby.IsUnique()) //zastosowanie metody rozszerzającej dla CB
- Console.WriteLine("wszystkie liczby są unikalne");
- else
- Console.WriteLine("Nie wszystkie liczby są unikalne");
- Console.ReadLine();
- }
- }
- }
- ----------------------- zad 6--------------------
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- namespace KolekcjaUnikalnaLock
- {
- public static class Extensions
- {
- public static bool IsUnique(this List<int> lista)
- {
- if (lista.Distinct().Count() == lista.Count()) //jeżeli liczba unikalnych jest równa liczbie wszystkich to wszystkie są unikalne.
- {
- return true;
- }
- // Console.WriteLine($"Liczba disctinct: {lista.Distinct().Count()} jest różna od liczby elementów: {lista.Count()}");
- return false;
- }
- public static bool IsUnique(this ConcurrentBag<int> cb)
- {
- if (cb.Distinct().Count() == cb.Count()) return true;
- else return false;
- }
- public static bool IsUnique(this ConcurrentDictionary<int,int> cb)
- {
- if (cb.Distinct().Count() == cb.Count()) return true;
- else return false;
- }
- }
- }
- ---------------------
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- using System.Diagnostics;
- namespace KolekcjaUnikalnaLock
- {
- class Program
- {
- static void Main(string[] args)
- {
- object _Lock = new object();
- List<Thread> watki = new List<Thread>();
- ConcurrentDictionary <int,int> liczby = new ConcurrentDictionary <int,int>();
- Stopwatch sw = new Stopwatch();
- sw.Start();
- Random rand = new Random();
- for (int i = 0; i < 100; i++)
- {
- var watek = new Thread(() =>
- {
- for (int l = 0; l < 100; l++)
- {
- int liczba = rand.Next(10001);
- lock(_Lock){
- liczby[l]=liczba;
- }
- }
- });
- watki.Add(watek);
- watek.Start();
- }
- foreach (var watek in watki)
- {
- watek.Join();
- }
- sw.Stop();
- Console.WriteLine($"Upłynęło {sw.ElapsedMilliseconds} ms");
- if (liczby.IsUnique()) //zastosowanie metody rozszerzającej dla CB
- Console.WriteLine("wszystkie liczby są unikalne");
- else
- Console.WriteLine("Nie wszystkie liczby są unikalne");
- Console.ReadLine();
- }
- }
- }
- -------------------------zad 7 -------------------------
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- using System.Diagnostics;
- namespace queue
- {
- class Program
- {
- static void Main(string[] args)
- {
- var cq = new ConcurrentQueue <int>();
- List<Thread> watki = new List<Thread>();
- Random rand = new Random();
- //prodcent wkłada do kolekcji 100 elementów z losowymi odstępami do 500ms
- var producent = new Thread(() =>
- {
- for (int l = 0; l < 100; l++)
- {
- cq.Enqueue(l);
- Console.WriteLine($"Wkładam {l}");
- Thread.Sleep(rand.Next(500));
- }
- });
- producent.Start();
- for (int i = 0; i < 10; i++)
- {
- var watek = new Thread(() =>
- {
- for (int l = 0; l < 10; l++)
- {
- try
- {
- int result2;
- bool udaloSie = false;
- while(udaloSie = cq.TryDequeue(out result2))
- Console.WriteLine($"Odbieram {result2} ");
- }
- catch (Exception ex)
- {
- Console.WriteLine("Wyjątek przy odebraniu: ", ex.Message);
- }
- Thread.Sleep(rand.Next(5000));
- }
- });
- watki.Add(watek);
- watek.Start();
- }
- foreach (var watek in watki)
- {
- watek.Join();
- }
- producent.Join();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement