N
Nguyen Duy Hoa -Mr.-
Guest
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
var list = new int[10].Select((t, i) => new Page { Index = i, Name = "Page" }).ToList();
#region Solution#1
var inputQueue = new BlockingCollection<Tuple<Page, int>>();
var queueResults = new BlockingCollection<Tuple<Page, int>>();
for (int i = 0; i < list.Count; i++)
{
var item = list;
inputQueue.Add(Tuple.Create(item, i));
}
inputQueue.CompleteAdding();
var producers = Enumerable.Range(0, Environment.ProcessorCount)
.Select(_ => Task.Run(() =>
{
foreach (var item in inputQueue.GetConsumingEnumerable())
{
queueResults.Add(Tuple.Create(item.Item1, item.Item2));
}
})).ToArray();
Task.WaitAll(producers);
queueResults.CompleteAdding();
queueResults.OrderBy(t => t.Item2).ToList().ForEach(t =>
{
Console.WriteLine(t.Item1);
});
#endregion
#region Solution#2
List<Tuple<int, string>> listRs = new List<Tuple<int, string>>();
Parallel.ForEach(list, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, item =>
{
listRs.Add(new Tuple<int, string>(item.Index, item.ToString()));
});
listRs.OrderBy(t => t.Item1).ToList().ForEach(t =>
{
Console.WriteLine(t.Item2);
});
#endregion
Console.ReadLine();
}
}
public class Page
{
public int Index { get; set; }
public string Name { get; set; }
public override string ToString()
{
return string.Format("{0} - {1}", Name, Index);
}
}
}
I need an advice, what solution I should choose?
Continue reading...
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
var list = new int[10].Select((t, i) => new Page { Index = i, Name = "Page" }).ToList();
#region Solution#1
var inputQueue = new BlockingCollection<Tuple<Page, int>>();
var queueResults = new BlockingCollection<Tuple<Page, int>>();
for (int i = 0; i < list.Count; i++)
{
var item = list;
inputQueue.Add(Tuple.Create(item, i));
}
inputQueue.CompleteAdding();
var producers = Enumerable.Range(0, Environment.ProcessorCount)
.Select(_ => Task.Run(() =>
{
foreach (var item in inputQueue.GetConsumingEnumerable())
{
queueResults.Add(Tuple.Create(item.Item1, item.Item2));
}
})).ToArray();
Task.WaitAll(producers);
queueResults.CompleteAdding();
queueResults.OrderBy(t => t.Item2).ToList().ForEach(t =>
{
Console.WriteLine(t.Item1);
});
#endregion
#region Solution#2
List<Tuple<int, string>> listRs = new List<Tuple<int, string>>();
Parallel.ForEach(list, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, item =>
{
listRs.Add(new Tuple<int, string>(item.Index, item.ToString()));
});
listRs.OrderBy(t => t.Item1).ToList().ForEach(t =>
{
Console.WriteLine(t.Item2);
});
#endregion
Console.ReadLine();
}
}
public class Page
{
public int Index { get; set; }
public string Name { get; set; }
public override string ToString()
{
return string.Format("{0} - {1}", Name, Index);
}
}
}
I need an advice, what solution I should choose?
Continue reading...