zoukankan      html  css  js  c++  java
  • 多线程15-ReaderWriterLockSlim

        class Program
        {
            static void Main()
            {
                new Thread(Read) { IsBackground = true }.Start();
                new Thread(Read) { IsBackground = true }.Start();
                new Thread(Read) { IsBackground = true }.Start();
                new Thread(Read) { IsBackground = true }.Start();
                new Thread(Read) { IsBackground = true }.Start();
                new Thread(() => Write("T1")) { IsBackground = true }.Start();
                new Thread(() => Write("T2")) { IsBackground = true }.Start();
                Thread.Sleep(TimeSpan.FromSeconds(30));
            }
            static ReaderWriterLockSlim rwl = new ReaderWriterLockSlim();
            static Dictionary<intint> items = new Dictionary<intint>();
            static void Read()
            {
                Console.WriteLine("Readind contents of a dictionary");
                while (true)
                {
                    try
                    {
                        rwl.EnterReadLock();
                        foreach (var item in items.Keys)
                        {
                            Thread.Sleep(TimeSpan.FromSeconds(0.1));
                            Console.WriteLine(items[item]);
                        }
                    }
                    finally
                    {
                        rwl.ExitReadLock();
                    }
                }
            }
            static void Write(string threadName)
            {
                while (true)
                {
                    try
                    {
                        int newKey = new Random().Next(250);
                        rwl.EnterUpgradeableReadLock();
                        if (!items.ContainsKey(newKey))
                        {
                            try
                            {
                                rwl.EnterWriteLock();
                                items[newKey] = newKey;
                                Console.WriteLine("New Key {0} is added to a dictionary by a {1}", newKey, threadName);
                            }
                            finally
                            {
                                rwl.ExitWriteLock();
                            }
                        }
                        Thread.Sleep(TimeSpan.FromSeconds(0.1));
                    }
                    finally
                    {
                        rwl.ExitUpgradeableReadLock();
                    }
                }
            }
        }
  • 相关阅读:
    jquer 的简输出
    jQuery 效果999动画 延迟
    Windows Mobile开发环境搭建指南
    使用.NET 框架压缩版开发Windows Mobile 2003 for Smartphone
    SmartPhone 2003 手机编程实战之一[转载]
    Smartphone移动开发—自己开发一个天气预报服务 (转载)
    101个微软提供的Visual Studio 2005示例
    基于 Windows Mobile 的 Pocket PC 和 Smartphone 的开发工具简介
    手机耐用绝对秘密方法
    Cheese 游戏编程:第 4 部分 (转自MSDN)
  • 原文地址:https://www.cnblogs.com/shidengyun/p/5608115.html
Copyright © 2011-2022 走看看