using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace s
{
class Program
{
static Int64 sumValue = 0;
static List<int> randomList = new List<int>();
static Semaphore sem = new Semaphore(1, 1);
static void AddToResult(int toAdd)
{
sem.WaitOne();
sumValue += toAdd;
sem.Release();
}
static void FillList(int Length)
{
var rand = new Random();
for (int i = 0; i < Length; i++)
{
randomList.Add(rand.Next(1, 5000));
}
}
static void PartialSum(int Index, int ToSum)
{
int sum = 0;
int range = Index + ToSum;
for (int i = Index; i < range; i++)
{
sum += randomList[i];
}
AddToResult(sum);
}
static List<int> SetChunk(int AmmontOfThreads)
{
if (AmmontOfThreads == 0)
throw new Exception("Ammount of Threads cannot be zero");
List<int> chunks = new List<int>();
int toCover = randomList.Count;
double ch = randomList.Count / AmmontOfThreads;
for (int i = 0; i < AmmontOfThreads; i++)
{
if (i == AmmontOfThreads - 1)
{
chunks.Add(toCover);
}
else
{
int p = (int)Math.Floor(ch);
chunks.Add(p);
toCover -= p;
}
}
return chunks;
}
static private List<int> SetIndexes(List<int> Chunks)
{
var listOfIndex = new List<int>();
int currentIndex = 0;
foreach (int chunk in Chunks)
{
listOfIndex.Add(currentIndex);
currentIndex += chunk;
}
return listOfIndex;
}
static private List<Thread> SetThreads(List<int> listOfIndex,List<int> Chunks)
{
var threadList = new List<Thread>();
for (int i = 0; i < listOfIndex.Count; i++)
{
int localIndex = i;
var t = new Thread(() => PartialSum(listOfIndex[localIndex], Chunks[localIndex]));
t.Start();
threadList.Add(t);
}
return threadList;
}
static void Main(string[] args)
{
const int arraySize = 10000;
const int numberOfThreads = 5;
var Chunks = SetChunk(10);
FillList(arraySize);
Chunks = SetChunk(numberOfThreads);
List<int> listOfIndex = SetIndexes(Chunks);
List<Thread> threadList = SetThreads(listOfIndex, Chunks);
foreach (var t in threadList)
t.Join();
Console.WriteLine("WielowÄ…tkowo: ");
Console.WriteLine(sumValue);
Console.WriteLine("Sekwencyjnie: ");
Console.WriteLine(randomList.Sum());
}
}
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}