Advertisement
ivandrofly

Parallel Programming in .NET and C# 4 [BASIC]

Mar 22nd, 2014
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4.  
  5. namespace Parallel_Programming
  6. {
  7.     internal class Program
  8.     {
  9.         private static void Main(string[] args)
  10.         {
  11.             Action<Action> measure = (body) =>
  12.                 {
  13.                     var startTime = DateTime.Now;
  14.                     body();
  15.                     Console.WriteLine("{0} {1}", DateTime.Now - startTime, Thread.CurrentThread.ManagedThreadId);
  16.                 };
  17.  
  18.             Action calcJob = () => { for (int i = 0; i < 350000000; i++);};
  19.             measure(() =>
  20.             {
  21.                 var tasks = new[]
  22.                 {
  23.                     Task.Factory.StartNew(() => measure(calcJob)),
  24.                     Task.Factory.StartNew(() => measure(calcJob)),
  25.                     Task.Factory.StartNew(() => measure(calcJob)),
  26.                     Task.Factory.StartNew(() => measure(calcJob)),
  27.                     Task.Factory.StartNew(() => measure(calcJob)),
  28.                     Task.Factory.StartNew(() => measure(calcJob))
  29.                 };
  30.                 Task.WaitAll(tasks);
  31.             });
  32.             // CTRL+F5
  33.         }
  34.     }
  35. }
  36. // Watch: https://www.youtube.com/watch?v=r1FbKiHYHcw
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement