代码改变世界

简介4种同步方法的实现

2013-09-06 14:00  随风浪迹天涯  阅读(1089)  评论(3编辑  收藏  举报

问题重现

static void Main()
    {

      Thread[] theads = new Thread[10];
      Printer p = new Printer();
      for (int i = 0; i < 10; i++)
      {
        theads[i] = new Thread(new ThreadStart(p.DisplayDigites));
        theads[i].Name = string.Format("thread work: {0}", i);
      }

      foreach (Thread t in theads)
        t.Start();
    }

新建一个Printer类,

public class Printer
  {
    public void DisplayDigites()
    {
      Console.WriteLine("{0} is executing DisplayDigites", Thread.CurrentThread.Name);
      Console.Write("Your Number are:");
      for (int i = 0; i < 10; i++)
      {

        Console.Write("{0},", i);
      }
      Console.WriteLine();

    }
  }

得到结果是:

很明显 这不是我们想要的结果。可能原因很多人都知道,是因为同步问题。那我们怎么解决这个同步问题呢?下面有4种方法,可以选择。

一。使用C#的lock 关键字

我们只需要在输出的方法上面加上lock 关键字就可以了。

 public void DisplayDigites()
    {
      lock (this)
      {
        Console.WriteLine("{0} is executing DisplayDigites", Thread.CurrentThread.Name);
        Console.Write("Your Number are:");
        for (int i = 0; i < 10; i++)
        {
          Console.Write("{0},", i);
        }
      }
      Console.WriteLine();
    }

得到的结果是:

这样就得到了我们的期望。当然 更常见的是,我们需要定义一个常见来替换this,如下表示:

private object obj = new object();
    public void DisplayDigites()
    {
      lock (obj)
      {
        Console.WriteLine("{0} is executing DisplayDigites", Thread.CurrentThread.Name);
        Console.Write("Your Number are:");
        for (int i = 0; i < 10; i++)
        {
          Console.Write("{0},", i);
        }
      }
      Console.WriteLine();
    }

结果也是一样。

二。使用Monitor进行同步

 private object obj = new object();
    public void DisplayDigites()
    {
      Monitor.Enter(obj);
      try
      {
        Console.WriteLine("{0} is executing DisplayDigites", Thread.CurrentThread.Name);
        Console.Write("Your Number are:");
        for (int i = 0; i < 10; i++)
        {
          Console.Write("{0},", i);
        }
      }
      finally
      {
        Monitor.Exit(obj);
      }
      Console.WriteLine();
    }
  }

三。使用[Synchronization]特性进行同步

这个很简单,只需要把这个特性添加到类上面就好了。但是还需要实现ContextBoundObject。。实现code 如下:

[Synchronization]
  public class Printer:ContextBoundObject
  {
    public void DisplayDigites()
    {
      Console.WriteLine("{0} is executing DisplayDigites", Thread.CurrentThread.Name);
      Console.Write("Your Number are:");
      for (int i = 0; i < 10; i++)
      {
        Console.Write("{0},", i);
      }
      Console.WriteLine();
    }
  }