Advertisement
Fhernd

SincronizacionBanda.cs

Jul 11th, 2015
1,112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.47 KB | None | 0 0
  1. // OrtizOL - xCSw - http://ortizol.blogspot.com
  2.  
  3. using System;
  4. using System.Threading;
  5.  
  6. namespace Receta.Multithreading.R0207
  7. {
  8.     public class UsoBarrier
  9.     {
  10.         public static Barrier barrier = new Barrier(2,
  11.                     b => Console.WriteLine("Fin de la fase: {0}",
  12.                             b.CurrentPhaseNumber + 1));
  13.        
  14.         public static void Main()
  15.         {
  16.             Console.WriteLine(Environment.NewLine);
  17.            
  18.             Thread t1 = new Thread(() => Tocar("El guitarrista", "tocar su guitarra.", 5));
  19.             Thread t2 = new Thread(() => Tocar("El vocalista", "cantar su canción.", 5));
  20.            
  21.             // Inicio de la ejecución de los threads:
  22.             t1.Start();
  23.             t2.Start();
  24.         }
  25.        
  26.         public static void Tocar(String nombre, String mensaje, int espera)
  27.         {
  28.             for (int i = 1; i < 3; ++i)
  29.             {
  30.                 Console.WriteLine("----------------------------------------------");
  31.                 // Espera:
  32.                 Thread.Sleep(TimeSpan.FromSeconds(espera));
  33.                
  34.                 Console.WriteLine("{0} empieza a {1}", nombre, mensaje);
  35.                
  36.                 // Espera:
  37.                 Thread.Sleep(TimeSpan.FromSeconds(espera));
  38.                
  39.                 Console.WriteLine("{0} finaliza de {1}", nombre, mensaje);
  40.                
  41.                 barrier.SignalAndWait();
  42.             }
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement