• 读写锁操作(ReaderWriterLockSlim)


        
    (1)

    static void WriterMethod(object writer)
    {
        Console.WriteLine("Writer {0} acquired the lock", writer);
        for (int i = 0; i < items.Count; i++)
        {
            items[i]++;
            Console.Write(items[i]);
        }
        Console.WriteLine("");
        Console.WriteLine("Writer {0} finished", writer);
    }
    
    private static List<int> items = new List<int>() { 0, 1, 2, 3, 4, 5 };
    
    static void Main()
    {
        WriterMethod(0);
        WriterMethod(1);
    
    }
    

    image
    没问题
    (2)换成线程
    for (int i = 0; i < 2; i++)
    {
        Thread a = new Thread(WriterMethod);
        a.Start(i);
    }
    

    顺序出错了
    image

    (3)使用读写锁操作ReaderWriterLockSlim
    使用EnterWriteLock锁定写操作,恢复正常,必须手动退出锁模式,否则将会永远独占ExitWriteLock

      static void WriterMethod(object writer)
      {
          rwl.EnterWriteLock();
          Console.WriteLine("Writer {0} acquired the lock", writer);
          for (int i = 0; i < items.Count; i++)
          {
              items[i]++;
              Console.Write(items[i]);
          }
          Console.WriteLine("");
          Console.WriteLine("Writer {0} finished", writer);
          rwl.ExitWriteLock();
      }
      private static ReaderWriterLockSlim rwl = new
    ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
    


    (4)设置锁超时时间
    使用Try方法是可以设置锁时间,如果超时的话,则继续执行,但此时则未进入锁定状态,无法用ExitWriteLock退出,可以用
    RecursiveWriteCount属性来判断

    static void WriterMethod(object writer)
    {
        rwl.TryEnterWriteLock(50);
        Console.WriteLine("Writer {0} acquired the lock", writer);
        for (int i = 0; i < items.Count; i++)
        {
            items[i]++;
            Console.Write(items[i]);
            Thread.Sleep(50);
        }
        Console.WriteLine("");
        Console.WriteLine("Writer {0} finished", writer);
        if(rwl.RecursiveWriteCount>0)
        rwl.ExitWriteLock();
    }
    
    结果照样混乱
    image

    (5)读写操作(单一执行)

    读写操作同时进行
    for (int i = 0; i < 2; i++)
    {
        Thread a = new Thread(WriterMethod);
        Thread b = new Thread(ReaderMethod);
        b.Start(i);
        a.Start(i);
        
    }
    

    static void ReaderMethod(object reader)
    {
    
        for (int i = 0; i < items.Count; i++)
        {
            Console.WriteLine("reader {0}, loop: {1}, item: {2}",
                  reader, i, items[i]);
           
        }
        }
    

    结果是混乱的,无法预测

    image

    进入读操作

    static void ReaderMethod(object reader)
    {
        rwl.EnterReadLock();
        for (int i = 0; i < items.Count; i++)
        {
            Console.WriteLine("reader {0}, loop: {1}, item: {2}",
                  reader, i, items[i]);
         
        }
        rwl.ExitReadLock();
       
    }
    

    现在读写操作是单一执行的,不可能同时处于读写状态
    image
    同样EnterReadLock也有Try方法


    另外参见
    http://www.cnblogs.com/lucifer1982/archive/2008/12/07/1349437.html

  • 相关阅读:
    将CMD内的显示内容输出到txt文件
    上传项目到Github
    利用百度地图API,在浏览器中找到自己的位置
    操作系统相关知识
    字符串的全排列
    二维数组做函数参数、指向指针的指针做函数参数
    strstr函数与strcmp函数
    华为机试题——数组排序,且奇数存在奇数位置,偶数存在偶数位置
    单链表的建立,插入,显示,查找删除以及反转
    《深入了解 Linq to SQL》之对象的标识 —— 麦叔叔呕心呖血之作
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1734651.html
Copyright © 2020-2023  润新知