多线程14-Barrier
Posted shidengyun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程14-Barrier相关的知识,希望对你有一定的参考价值。
class Program
{
static Barrier barrier = new Barrier(2, b => Console.WriteLine("End of phase {0}", b.CurrentPhaseNumber + 1));
static void PalyMusic(string name, string message, int second)
{
for (var i = 1; i <= 3; i++)
{
Console.WriteLine("----------------------------------");
Thread.Sleep(TimeSpan.FromSeconds(second));
Console.WriteLine("{0} Start to {1}", name, message);
Thread.Sleep(TimeSpan.FromSeconds(second));
Console.WriteLine("{0} finisheds to {1}", name, message);
barrier.SignalAndWait();
}
}
static void Main()
{
var t1 = new Thread(() => PalyMusic("the guitarist", "paly an amzaing solo", 5));
var t2 = new Thread(() => PalyMusic("the singer", "sing his song", 2));
t1.Start();
t2.Start();
}
}
{
static Barrier barrier = new Barrier(2, b => Console.WriteLine("End of phase {0}", b.CurrentPhaseNumber + 1));
static void PalyMusic(string name, string message, int second)
{
for (var i = 1; i <= 3; i++)
{
Console.WriteLine("----------------------------------");
Thread.Sleep(TimeSpan.FromSeconds(second));
Console.WriteLine("{0} Start to {1}", name, message);
Thread.Sleep(TimeSpan.FromSeconds(second));
Console.WriteLine("{0} finisheds to {1}", name, message);
barrier.SignalAndWait();
}
}
static void Main()
{
var t1 = new Thread(() => PalyMusic("the guitarist", "paly an amzaing solo", 5));
var t2 = new Thread(() => PalyMusic("the singer", "sing his song", 2));
t1.Start();
t2.Start();
}
}
以上是关于多线程14-Barrier的主要内容,如果未能解决你的问题,请参考以下文章