线程基础示例代码之ReaderWriterLockSlim

2018-06-26  本文已影响0人  JameLee

ReaderWriterLockSlim 类可用于多个线程同时读,但同一时间,只能有一个线程写的情况

ReaderWriterLockSlim 类支持三种锁定模式:ReadWriteUpgradeableRead。这三种模式对应的方法分别为

ReaderWriterLockSlim 类似于 ReaderWriterLock, 只是简化了递归、升级和降级锁定状态的规则。ReaderWriterLockSlim可避免很多潜在的可能会产生死锁的情况。此外,ReaderWriterLockSlim 性能明显优于 ReaderWriterLock

需要注意的点

一个 ReaderWriterLockSlim 可以是以下四种状态之一

示例代码如下

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

public class App {
    public class SynchronizedCache {
        private ReaderWriterLockSlim cacheLock = new ReaderWriterLockSlim();
        private Dictionary<int, string> innerCache = new Dictionary<int, string>();
        public int Count { get { return innerCache.Count; } }

        public string Read(int key) {
            cacheLock.EnterReadLock();
            try {
                return innerCache[key];
            } finally {
                cacheLock.ExitReadLock();
            }
        }

        public void Add(int key, string value) {
            cacheLock.EnterWriteLock();
            try {
                innerCache.Add(key, value);
            } finally {
                cacheLock.ExitWriteLock();
            }
        }

        public bool AddWithTimeout(int key, string value, int timeout) {
            if (cacheLock.TryEnterWriteLock(timeout)) {
                try {
                    innerCache.Add(key, value);
                } finally {
                    cacheLock.ExitWriteLock();
                }
                return true;
            } else {
                return false;
            }
        }

        public AddOrUpdateStatus AddOrUpdate(int key, string value) {
            cacheLock.EnterUpgradeableReadLock();
            try {
                if (innerCache.TryGetValue(key, out string result)) {
                    if (result == value) {
                        return AddOrUpdateStatus.Unchanged;
                    } else {
                        cacheLock.EnterWriteLock();
                        try {
                            innerCache[key] = value;
                        } finally {
                            cacheLock.ExitWriteLock();
                        }
                        return AddOrUpdateStatus.Updated;
                    }
                } else {
                    cacheLock.EnterWriteLock();
                    try {
                        innerCache.Add(key, value);
                    } finally {
                        cacheLock.ExitWriteLock();
                    }
                    return AddOrUpdateStatus.Added;
                }
            } finally {
                cacheLock.ExitUpgradeableReadLock();
            }
        }

        public void Delete(int key) {
            cacheLock.EnterWriteLock();
            try {
                innerCache.Remove(key);
            } finally {
                cacheLock.ExitWriteLock();
            }
        }

        public enum AddOrUpdateStatus {
            Added,
            Updated,
            Unchanged
        };

        ~SynchronizedCache() {
            if (cacheLock != null) cacheLock.Dispose();
        }
    }

    public static void Main() {
        var sc = new SynchronizedCache();
        var tasks = new List<Task>();
        int itemsWritten = 0;

        // Execute a writer.
        tasks.Add(Task.Run(() => {
            String[] vegetables = {
                "broccoli", "cauliflower",
                "carrot", "sorrel", "baby turnip",
                "beet", "brussel sprout",
                "cabbage", "plantain",
                "spinach", "grape leaves",
                "lime leaves", "corn",
                "radish", "cucumber",
                "raddichio", "lima beans"
            };
            for (int ctr = 1; ctr <= vegetables.Length; ctr++)
                sc.Add(ctr, vegetables[ctr - 1]);

            itemsWritten = vegetables.Length;
            Console.WriteLine("Task {0} wrote {1} items\n", Task.CurrentId, itemsWritten);
        }));

        // Execute two readers, one to read from first to last and the second from last to first.
        for (int ctr = 0; ctr <= 1; ctr++) {
            bool desc = Convert.ToBoolean(ctr);
            tasks.Add(Task.Run(() => {
                int start, last, step;
                int items;
                do {
                    String output = String.Empty;
                    items = sc.Count;
                    if (!desc) {
                        start = 1;
                        step = 1;
                        last = items;
                    } else {
                        start = items;
                        step = -1;
                        last = 1;
                    }

                    for (int index = start; desc ? index >= last : index <= last; index += step)
                        output += String.Format("[{0}] ", sc.Read(index));

                    Console.WriteLine("Task {0} read {1} items: {2}\n", Task.CurrentId, items, output);
                } while (items < itemsWritten | itemsWritten == 0);
            }));
        }
        // Execute a red/update task.
        tasks.Add(Task.Run(() => {
            Thread.Sleep(100);
            for (int ctr = 1; ctr <= sc.Count; ctr++) {
                String value = sc.Read(ctr);
                if (value == "cucumber")
                    if (sc.AddOrUpdate(ctr, "green bean") != SynchronizedCache.AddOrUpdateStatus.Unchanged)
                        Console.WriteLine("Changed 'cucumber' to 'green bean'");
            }
        }));

        // Wait for all three tasks to complete.
        Task.WaitAll(tasks.ToArray());

        // Display the final contents of the cache.
        Console.WriteLine();
        Console.WriteLine("Values in synchronized cache: ");
        for (int ctr = 1; ctr <= sc.Count; ctr++)
            Console.WriteLine("   {0}: {1}", ctr, sc.Read(ctr));
    }
}

在实际项目中,以上的 SynchronizedCache 类完全可以使用 ConcurrentDictionary<int, string> 来替代



至此,本节内容讲解完毕。
欢迎关注公众号【嘿嘿的学习日记】,所有的文章,都会在公众号首发,Thank you~

公众号二维码.jpg
上一篇下一篇

猜你喜欢

热点阅读