Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/8240395bbd65c0eb4db8 to your computer and use it in GitHub Desktop.
Save Fhernd/8240395bbd65c0eb4db8 to your computer and use it in GitHub Desktop.
Demostración de uso de la clase `Barrier` en C#.
// OrtizOL - xCSw - http://ortizol.blogspot.com
using System;
using System.Threading;
namespace Receta.Multithreading.R0207
{
public class UsoBarrier
{
public static Barrier barrier = new Barrier(2,
b => Console.WriteLine("Fin de la fase: {0}",
b.CurrentPhaseNumber + 1));
public static void Main()
{
Console.WriteLine(Environment.NewLine);
Thread t1 = new Thread(() => Tocar("El guitarrista", "tocar su guitarra.", 5));
Thread t2 = new Thread(() => Tocar("El vocalista", "cantar su canción.", 5));
// Inicio de la ejecución de los threads:
t1.Start();
t2.Start();
}
public static void Tocar(String nombre, String mensaje, int espera)
{
for (int i = 1; i < 3; ++i)
{
Console.WriteLine("----------------------------------------------");
// Espera:
Thread.Sleep(TimeSpan.FromSeconds(espera));
Console.WriteLine("{0} empieza a {1}", nombre, mensaje);
// Espera:
Thread.Sleep(TimeSpan.FromSeconds(espera));
Console.WriteLine("{0} finaliza de {1}", nombre, mensaje);
barrier.SignalAndWait();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment