Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Parallel_Programming
- {
- internal class Program
- {
- private static void Main(string[] args)
- {
- Action<Action> measure = (body) =>
- {
- var startTime = DateTime.Now;
- body();
- Console.WriteLine("{0} {1}", DateTime.Now - startTime, Thread.CurrentThread.ManagedThreadId);
- };
- Action calcJob = () => { for (int i = 0; i < 350000000; i++);};
- Action ioJob = () => { Thread.Sleep(1000); };
- //measure(() =>
- //{
- // var tasks = Enumerable.Range(1, 10)
- // .Select(_ => Task.Factory.StartNew(() => measure(ioJob))).ToArray();
- // Task.WaitAll(tasks);
- //});
- // CTRL+F5
- //ThreadPool.SetMinThreads(5, 5);
- //Parallel.For(0, 10, _ => { measure(ioJob); });
- //ParallelEnumerable.Range(10, 10)
- // .WithDegreeOfParallelism(5)
- // .ForAll(_ => measure(ioJob));
- //Enumerable.Range(1, 10).ToList()
- // .AsParallel()
- // .WithDegreeOfParallelism(10)
- // .ForAll(_ => measure(ioJob));
- //var queue = new Queue<int>();
- var queue = new System.Collections.Concurrent.BlockingCollection<int>(100);
- var producers = Enumerable.Range(1, 3)
- .Select(_ => Task.Factory.StartNew(() =>
- {
- Enumerable.Range(1, 100)
- .ToList()
- .ForEach(i =>
- {
- queue.Add(i);
- Thread.Sleep(100);
- });
- })).ToArray();
- var consumers = Enumerable.Range(1, 2)
- .Select(_ => Task.Factory.StartNew(() =>
- {
- foreach (var item in queue.GetConsumingEnumerable())
- {
- Console.WriteLine(item);
- }
- })).ToArray();
- Task.WaitAll(producers);
- queue.CompleteAdding();
- Task.WaitAll(consumers);
- }
- }
- }
- // Watch: https://www.youtube.com/watch?v=r1FbKiHYHcw
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement