• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

C#的多线程机制初探(5)

发布: 2007-6-30 23:38 | 作者: admin | 来源: | 查看: 17次 | 进入软件测试论坛讨论

领测软件测试网     
    首先,我们定义一个被操作的对象的类Cell,在这个类里,有两个方法:ReadFromCell()和WriteToCell。消费者线程将调用ReadFromCell()读取cellContents的内容并且显示出来,生产者进程将调用WriteToCell()方法向cellContents写入数据。
  
    public class Cell
    {
      int cellContents; // Cell对象里边的内容
      bool readerFlag = false; // 状态标志,为true时可以读取,为false则正在写入
      public int ReadFromCell( )
      {
        lock(this) // Lock关键字保证了什么,请大家看前面对lock的介绍
        {
          if (!readerFlag)//如果现在不可读取
          {
            try
            {
              //等待WriteToCell方法中调用Monitor.Pulse()方法
              Monitor.Wait(this);
            }
            catch (SynchronizationLockException e)
            {
              Console.WriteLine(e);
            }
            catch (ThreadInterruptedException e)
            {
              Console.WriteLine(e);
            }
          }
          Console.WriteLine("Consume: {0}",cellContents);
          readerFlag = false; //重置readerFlag标志,表示消费行为已经完成
          Monitor.Pulse(this); //通知WriteToCell()方法(该方法在另外一个线程中执行,等待中)
        }
        return cellContents;
      }
  
      public void WriteToCell(int n)
      {
        lock(this)
        {
          if (readerFlag)
          {
            try
            {
              Monitor.Wait(this);
            }
            catch (SynchronizationLockException e)
            {
              //当同步方法(指Monitor类除Enter之外的方法)在非同步的代码区被调用
              Console.WriteLine(e);
            }
            catch (ThreadInterruptedException e)
            {
              //当线程在等待状态的时候中止
              Console.WriteLine(e);
            }
          }
          cellContents = n;
          Console.WriteLine("Produce: {0}",cellContents);
          readerFlag = true;
          Monitor.Pulse(this); //通知另外一个线程中正在等待的ReadFromCell()方法
        }
      }
    }
  
    下面定义生产者CellProd和消费者类CellCons,它们都只有一个方法ThreadRun(),以便在Main()函数中提供给线程的ThreadStart代理对象,作为线程的入口。
  
    public class CellProd
    {
      Cell cell; // 被操作的Cell对象
      int quantity = 1; // 生产者生产次数,初始化为1
  
      public CellProd(Cell box, int request)
      {
        //构造函数
        cell = box;
        quantity = request;
      }
      public void ThreadRun( )
      {
        for(int looper=1; looper<=quantity; looper++)
          cell.WriteToCell(looper); //生产者向操作对象写入信息
      }
    }
  
    public class CellCons
    {
      Cell cell;
      int quantity = 1;
  
      public CellCons(Cell box, int request)
      {
        cell = box;
        quantity = request;
      }
      public void ThreadRun( )
      {
        int valReturned;
        for(int looper=1; looper<=quantity; looper++)
          valReturned=cell.ReadFromCell( );//消费者从操作对象中读取信息
      }
    }
  
  

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网