.NET 4 System.Threading.Barrier 类

Visual Studio 2010 and .NET Framework 4 Training Kit中有个System.Threading.Barrier的Demo,通过Barrier Class我们可以控制线程的运行,做到线程同步的效果。

Barrier Class在使用上十分的简单,只要在Barrier的构造函数中传入participantCount(简单的说就是要等待的线程个数),并在要同步的点调用SignalAndWait方法就可以了。线程会在调用SignalAndWait之后暂停运行,等待所有参与的线程都到达了同步点才继续往下运行。

.NET 4 System.Threading.Barrier 类

举个例子来看,假设今天Charlie、Mac、Dennis三个人相约要去西雅图喝咖啡。由于三个人的住的地区不尽相同,且车子都需要加油,因此他们约在途中会经过的加油站待会合后一同前往。这样的情境我们可以通过Thread与Barrier用程序仿真出来。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace BarrierDemo
{
    class Program
    {
        static Barrier sync;
        static CancellationToken token;

        static void Main(string[] args)
        {
            var source = new CancellationTokenSource();
            token = source.Token;
            sync = new Barrier(3);

            var charlie = new Thread(() => DriveToBoston("Charlie", TimeSpan.FromSeconds(1))); charlie.Start();
            var mac = new Thread(() => DriveToBoston("Mac", TimeSpan.FromSeconds(2))); mac.Start();
            var dennis = new Thread(() => DriveToBoston("Dennis", TimeSpan.FromSeconds(3))); dennis.Start();

            //source.Cancel();

            charlie.Join();
            mac.Join();
            dennis.Join();

            Console.ReadKey();
        }

        static void DriveToBoston(string name, TimeSpan timeToGasStation)
        {
            try
            {
                Console.WriteLine("[{0}] Leaving House", name);

                // Perform some work
                Thread.Sleep(timeToGasStation);
                Console.WriteLine("[{0}] Arrived at Gas Station", name);

                // Need to sync here
                sync.SignalAndWait(token);

                // Perform some more work
                Console.WriteLine("[{0}] Leaving for Boston", name);
            }
            catch (OperationCanceledException)
            {
                Console.WriteLine("[{0}] Caravan was cancelled! Going home!", name);
            }
        }
    }
}

你可能感兴趣的:(reading)