Facebook
From Oskar, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. namespace s
  9. {
  10.     class Program
  11.     {
  12.         static Int64 sumValue = 0;
  13.         static List<int> randomList = new List<int>();
  14.         static Semaphore sem = new Semaphore(1, 1);
  15.  
  16.         static void AddToResult(int toAdd)
  17.         {
  18.             sem.WaitOne();
  19.             sumValue += toAdd;
  20.             sem.Release();
  21.         }
  22.         static void FillList(int Length)
  23.         {
  24.             var rand = new Random();
  25.             for (int i = 0; i < Length; i++)
  26.             {
  27.                 randomList.Add(rand.Next(1, 5000));
  28.             }
  29.         }
  30.  
  31.         static void PartialSum(int Index, int ToSum)
  32.         {
  33.             int sum = 0;
  34.             int range = Index + ToSum;
  35.  
  36.             for (int i = Index; i < range; i++)
  37.             {
  38.                 sum += randomList[i];
  39.             }
  40.  
  41.             AddToResult(sum);
  42.         }
  43.  
  44.         static List<int> SetChunk(int AmmontOfThreads)
  45.         {
  46.             if (AmmontOfThreads == 0)
  47.                 throw new Exception("Ammount of Threads cannot be zero");
  48.  
  49.             List<int> chunks = new List<int>();
  50.             int toCover = randomList.Count;
  51.             double ch = randomList.Count / AmmontOfThreads;
  52.  
  53.             for (int i = 0; i < AmmontOfThreads; i++)
  54.             {
  55.                 if (i == AmmontOfThreads - 1)
  56.                 {
  57.                     chunks.Add(toCover);
  58.                 }
  59.                 else
  60.                 {
  61.                     int p = (int)Math.Floor(ch);
  62.                     chunks.Add(p);
  63.                     toCover -= p;
  64.                 }
  65.             }
  66.  
  67.             return chunks;
  68.         }
  69.  
  70.         static private List<int> SetIndexes(List<int> Chunks)
  71.         {
  72.             var listOfIndex = new List<int>();
  73.             int currentIndex = 0;
  74.             foreach (int chunk in Chunks)
  75.             {
  76.                 listOfIndex.Add(currentIndex);
  77.                 currentIndex += chunk;
  78.             }
  79.  
  80.             return listOfIndex;
  81.         }
  82.         static private List<Thread> SetThreads(List<int> listOfIndex,List<int> Chunks)
  83.         {
  84.             var threadList = new List<Thread>();
  85.  
  86.             for (int i = 0; i < listOfIndex.Count; i++)
  87.             {
  88.                 int localIndex = i;
  89.                 var t = new Thread(() => PartialSum(listOfIndex[localIndex], Chunks[localIndex]));
  90.                 t.Start();
  91.                 threadList.Add(t);
  92.             }
  93.  
  94.             return threadList;
  95.         }
  96.  
  97.         static void Main(string[] args)
  98.         {
  99.             const int arraySize = 10000;
  100.             const int numberOfThreads = 5;
  101.  
  102.             var Chunks = SetChunk(10);          
  103.             FillList(arraySize);
  104.             Chunks = SetChunk(numberOfThreads);
  105.             List<int> listOfIndex = SetIndexes(Chunks);
  106.             List<Thread> threadList = SetThreads(listOfIndex, Chunks);
  107.            
  108.             foreach (var t in threadList)
  109.                 t.Join();
  110.  
  111.             Console.WriteLine("WielowÄ…tkowo: ");
  112.             Console.WriteLine(sumValue);
  113.             Console.WriteLine("Sekwencyjnie: ");
  114.             Console.WriteLine(randomList.Sum());
  115.         }
  116.  
  117.        
  118.     }
  119. }
  120.