Detailed explanation of using ConcurrentDictionary multi-thread synchronization dictionary collection example

零下一度
Release: 2017-06-24 09:48:24
Original
14287 people have browsed it

In the past period of time, most of my base classes used a combination of lock and Hashtable to implement conflict handling for caches in multi-threads. However, sometimes using these two combinations was not satisfactory. Occasionally, the collection has been added. Exception, the problem remained the same after multiple processing of the code. Finally, the ConcurrentDictionary multi-threaded synchronized dictionary collection introduced after .NET 4.0 was used, and the problem was successfully solved.

1. Use lock and Hashtable combination to achieve

In my base class, build the business object. Generally, you can use BLLFactory.Instance to get the application of the corresponding business object.

var result = BLLFactory<Customer>.Instance.FindFirst();
Console.WriteLine(result.ToJson());
Copy after login

So after using BLLFactory.Instance to construct the objects, put them in the HashTable. Since multi-thread conflict handling needs to be designed, the lock object needs to be used to implement locking. deal with.

HashTable represents a collection of key/value pairs. In the .NET Framework, Hashtable is a container provided by the System.Collections namespace. It is used to process and represent key-value pairs. The key can usually be used for quick search, and the key is case-sensitive; the value is used for Store the value corresponding to key. The key-value pairs in Hashtable are all of object type, so Hashtable can support any type of key-value pair, and any non-null object can be used as a key or value.

Using this method, occasionally multi-threaded access conflicts still occur on the Web side. For this reason, we can also use multi-threaded test code to test and reproduce the error,

            try{
                List<Thread> list = new List<Thread>();for (int i = 0; i < 10; i++)
                {
                    Thread thread = new Thread(() =>{var result = BLLFactory<Customer>.Instance.FindFirst();
                        Console.WriteLine(result.ToJson());
                        Console.WriteLine();
                    });

                    list.Add(thread);
                }for (int i = 0; i < list.Count; i++)
                {
                    list[i].Start();
                }
            }catch(Exception ex)
            {
                LogTextHelper.Error(ex);
            }
Copy after login

The error message obtained by the tracking code is as follows.

Therefore, as you can see from the above code, multi-thread conflict problems cannot occur even with lock(syncRoot).

2. Use ConcurrentDictionary instead of Hashtable

ConcurrentDictionary is one of a set of thread-safe collections launched by .net4.0, and was released together with it. ConcurrentStack, ConcurrentQueue and other types, their single-threaded versions (thread-unsafe, Queue, Stack, Dictionary) will definitely be familiar to us. ConcurrentDictionary<TKey, TValue> can be accessed by multiple threads at the same time and is thread-safe. Its usage is the same as Dictionary, but with some more methods. ConcurrentDictionary belongs to the System.Collections.Concurrent namespace.

The System.Collections.Concurrent namespace provides multiple thread-safe collection classes. When multiple threads are accessing a collection concurrently, these classes should be used instead of the corresponding types in the System.Collections and System.Collections.Generic namespaces ##.

ConcurrentDictionary class provides the following methods for processing collections

public bool TryAdd(TKey key, TValue value)public bool TryUpdate(TKey key, TValue newValue, TValue comparisonValue)public TValue this[TKey key] { get; set; }public TValue AddOrUpdate(TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)    
public TValue AddOrUpdate(TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)public TValue GetOrAdd(TKey key, TValue value)public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
Copy after login
Use ConcurrentDictionary to replace Hashtable, Let's take a look at the implementation code of the BLLFactory class as shown below.

    /// <summary>/// 对业务类进行构造的工厂类/// </summary>/// <typeparam name="T">业务对象类型</typeparam>public class BLLFactory<T> where T : class{//采用ConcurrentDictionary线程安全的集合类来缓存,替代Hashtableprivate static ConcurrentDictionary<string, object> conCurrentCache = new ConcurrentDictionary<string, object>(); /// <summary>/// 创建或者从缓存中获取对应业务类的实例/// </summary>public static T Instance
        {get{string CacheKey = typeof(T).FullName;return (T)conCurrentCache.GetOrAdd(CacheKey, s =>{var bll = Reflect<T>.Create(typeof(T).FullName, typeof(T).Assembly.GetName().Name); //反射创建,并缓存return bll;
                });
            }
        }
    }
Copy after login
We can see that the code has been simplified a lot, and using the previous multi-threaded test code, the data can be obtained smoothly without exception.

#The running code can be implemented smoothly, and the multi-threaded access exception that occurred before using Hashtable will not occur.

The above is the introduction of ConcurrentDictionary to replace Hashtable for multi-threaded object caching. When the problem can be solved smoothly, it is found that the access efficiency is also improved compared to before, killing two birds with one stone.

The above is the detailed content of Detailed explanation of using ConcurrentDictionary multi-thread synchronization dictionary collection example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template