Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- zad 2
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Collections.Concurrent;
- namespace zad2,3
- {
- class Program
- {
- static void Main(string[] args)
- {
- Stopwatch zegarek = new Stopwatch();
- List<Thread> watki = new List<Thread>();
- ConcurrentBag<int> liczby = new ConcurrentBag<int>();
- Random rand = new Random();
- 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);
- zegarek.Start();
- watek.Start();
- }
- foreach (var watek in watki)
- {
- watek.Join();
- }
- zegarek.Stop();
- Console.WriteLine("Liczba elementów w liście zwykłej: {0}", liczby.Count);
- Console.WriteLine("Upłynęło {0} ms", zegarek.ElapsedMilliseconds);
- Console.ReadLine();
- }
- }
- }
- zad4
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace zad2
- {
- class Program
- {
- static object block = new object();
- static void Main(string[] args)
- {
- Stopwatch zegarek = new Stopwatch();
- List<Thread> watki = new List<Thread>();
- List<int> liczby = new List<int>(1000000);
- Random rand = new Random();
- for (int i = 0; i < 100; i++)
- {
- var watek = new Thread(() =>
- {
- lock (block)
- {
- for (int l = 0; l < 10000; l++)
- liczby.Add(rand.Next());
- }
- });
- watki.Add(watek);
- zegarek.Start();
- watek.Start();
- }
- foreach (var watek in watki)
- {
- watek.Join();
- }
- zegarek.Stop();
- Console.WriteLine("Liczba elementów w liście zwykłej: {0}", liczby.Count);
- Console.WriteLine("Upłynęło {0} ms", zegarek.ElapsedMilliseconds);
- 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
- {
- public static class Extensions
- {
- public static bool IsUnique(this List<Thread> 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;
- }
- static object block = new object();
- static void Main(string[] args)
- {
- List<Thread> watki = new List<Thread>();
- ConcurrentBag<int>liczby = new ConcurrentBag<int>();
- int liczba=0;
- 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++)
- lock (block)
- {
- liczba = rand.Next(10001);
- }
- lock (block)
- {
- 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 {0} ms",sw.ElapsedMilliseconds);
- 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;
- using System.Diagnostics;
- namespace KolekcjaDictionary
- {
- public static class Extensions
- {
- public static bool IsUnique(this List<Thread> 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 ConcurrentDictionary<int, object> cd)
- {
- if (cd.Distinct().Count() == cd.Count()) return true;
- else return false;
- }
- static object blokowacz = new object();
- static void Main(string[] args)
- {
- List<Thread> watki = new List<Thread>();
- //ConcurrentBag<int> liczby = new ConcurrentBag<int>();
- var liczby = new ConcurrentDictionary<int, object>();
- int liczba = 0;
- Stopwatch zegarek = new Stopwatch();
- zegarek.Start();
- Random rand = new Random();
- for (int i = 0; i < 100; i++)
- {
- var watek = new Thread(() =>
- {
- for (int l = 0; l < 100; l++)
- lock (blokowacz)
- {
- liczba = rand.Next(10001);
- }
- lock (blokowacz)
- {
- //while (liczby.Any(x => x == rand.Next(10001)))
- //{
- liczba = rand.Next(10001);
- //};
- }
- liczby.TryAdd(liczba,null);
- });
- watki.Add(watek);
- watek.Start();
- }
- foreach (var watek in watki)
- {
- watek.Join();
- }
- zegarek.Stop();
- Console.WriteLine("Upłynęło {0} ms", zegarek.ElapsedMilliseconds);
- 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();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement