Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // OrtizOL - xCSw - http://ortizol.blogspot.com
  2.  
  3. using System;
  4. using System.Diagnostics;
  5. using System.Threading;
  6.  
  7. namespace Receta.CSharp.R0303
  8. {
  9.     public class ParalelismoPoolThreads
  10.     {
  11.         public static void Main()
  12.         {
  13.             Console.WriteLine(Environment.NewLine);
  14.            
  15.             // Número de operaciones a ejecutar:
  16.             const int numeroOperaciones = 500;
  17.            
  18.             // Creación de cronómetro:
  19.             Stopwatch sw = new Stopwatch();
  20.            
  21.             // Inicio del cronómetro para medir el tiempo
  22.             // que toma la creación de threads:
  23.             sw.Start();
  24.             UsoThreads(numeroOperaciones);
  25.             sw.Stop();
  26.            
  27.             // Obtiene el tiempo transcurrido:
  28.             TimeSpan ts = sw.Elapsed;
  29.            
  30.             // Formato de la representación del tiempo
  31.             // transcurrido:
  32.             string formatoTiempo = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
  33.                                     ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
  34.                                    
  35.             // Muestro tiempo medido en la creación de de threads:
  36.             Console.WriteLine("\nTiempo cronometrizado creación de threads: {0}", formatoTiempo);
  37.            
  38.             Console.WriteLine(Environment.NewLine);
  39.            
  40.             // Reestablece el cronómetro:
  41.             sw.Reset();
  42.            
  43.             // Inicia de nuevo la medición. Esta vez para
  44.             // el pool de threads:
  45.             sw.Start();
  46.             UsoPoolThreads(numeroOperaciones);
  47.             sw.Stop();
  48.            
  49.             // Obtiene el tiempo transcurrido:
  50.             ts = sw.Elapsed;
  51.            
  52.             // Formato de la representación del tiempo
  53.             // transcurrido:
  54.             formatoTiempo = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
  55.                                     ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
  56.            
  57.             Console.WriteLine("\nTiempo cronometrizado pool de threads: {0}", formatoTiempo);
  58.            
  59.             Console.WriteLine(Environment.NewLine);
  60.         }
  61.        
  62.         private static void UsoThreads(int numeroOperaciones)
  63.         {
  64.             using (CountdownEvent contador = new CountdownEvent(numeroOperaciones))
  65.             {
  66.                 Console.WriteLine ("Inicio de creación de threads para ejecutar operaciones asincrónicas...");
  67.                
  68.                 for (int i = 1; i <= numeroOperaciones; ++i)
  69.                 {
  70.                     Thread t = new Thread( () => {
  71.                         Console.Write("{0}, ", Thread.CurrentThread.ManagedThreadId.ToString());
  72.                        
  73.                         Thread.Sleep(TimeSpan.FromSeconds(0.1));
  74.                         contador.Signal();
  75.                     });
  76.                    
  77.                     t.Start();
  78.                 }
  79.                
  80.                 contador.Wait();
  81.                 Console.WriteLine();
  82.             }
  83.         }
  84.        
  85.         private static void UsoPoolThreads(int numeroOperaciones)
  86.         {
  87.             using (CountdownEvent contador = new CountdownEvent(numeroOperaciones))
  88.             {
  89.                 Console.WriteLine("Inicio de pool de threads...");
  90.                
  91.                 for (int i = 1; i <= numeroOperaciones; ++i)
  92.                 {
  93.                     ThreadPool.QueueUserWorkItem( _ => {
  94.                         Console.Write("{0}, ", Thread.CurrentThread.ManagedThreadId);
  95.                        
  96.                         Thread.Sleep(TimeSpan.FromSeconds(0.1));
  97.                        
  98.                         contador.Signal();
  99.                     });
  100.                 }
  101.                
  102.                 contador.Wait();
  103.                 Console.WriteLine ();
  104.             }
  105.         }
  106.     }
  107. }