The foundation is the top priority ~ ConcurrentDictionary makes your multi-threaded code more elegant. concurrentdictionary

Source: Internet
Author: User

The foundation is the top priority ~ ConcurrentDictionary makes your multi-threaded code more elegant. concurrentdictionary

Back to directory

ConcurrentDictionary is. one of the thread security sets released by net4.0 is ConcurrentStack, ConcurrentQueue, and other types released together. Their Single-thread versions (thread unsafe, Queue, Stack, dictionary) We will not be unfamiliar, it can be said that it is often used, a class instance, there is an attribute is a Dictionary, we do not consider will use Dictionary, when this attribute is promoted to static (Class-level), we need to consider its thread security because it may be accessed by multiple threads at the same time. Of course, if this object isRead-OnlyDoes not matter thread security, but if this attribute isWrittenYou need to lock it. This code is often seen:

lock(obj){_dic[key]=value;}

Look, your code will have a very good lock block, not to mention whether it is beautiful, but in terms of performance, it cannot be received. We know that lock will lock other threads out, no matter whether it is read or write, it will be locked, and the performance is very good. Microsoft also saw its shortcomings, so it launched the System. collections. concurrent set. In this namespace, a batch of thread-safe objects are developed. Of course, the kernel is also similar to the lock mechanism, but Xiaowei must have done a lot of optimization, this is something we are sure.

If your previous method uses Dicationary, the modification is also convenient. You only need to use the adapter mode to process it.

Private readonly static ConcurrentDictionary <string, T> _ dic;
# Region IDictionary <string, ResultType> public void Add (string key, T value) {_ dic. tryAdd (key, value);} public bool ContainsKey (string key) {return _ dic. containsKey (key);} public ICollection <string> Keys {get {return _ dic. keys ;}} public bool Remove (string key) {T val; return _ dic. tryRemove (key, out val);} public bool TryGetValue (string key, out T value) {return _ dic. tryGetValue (key, out value);} public ICollection <T> Values {get {return _ dic. values ;}} public T this [string key] {get {return _ dic [key] ;}set {_ dic [key] = value ;}# endregion

How about it? The code without the lock block is much more beautiful!

Back to directory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.