Wonderful use of static thread in object Cache

Source: Internet
Author: User

The value of the static field marked by the thread is unique for each thread. To put it simply,It is only applied to static fields and is stored independently for each thread.

The requirements for Object Caching are as follows:

1. Some tool classes require certain resources, but do not want to create them too frequently.

2. multi-thread adaptation

An example is the StringBuilder class. The StringBuilder class uses char [] to store characters. We need to use StringBuilder to create strings, and we want to create them less frequently to avoid Memory pressure.

The implementation of StringBuilderCache in. NET Framework 4.5.1 is as follows:

MAX_BUILDER_SIZE = StringBuilder Acquire (capacity = (capacity <= (sb! = (Capacity <= (sb. Capacity <= result =

For versions with complete comments, see: http://www.projky.com/dotnet/4.5.1/System/Text/StringBuilderCache.cs.html

The example of StringBuidlerCache class is the String. Join method, you can refer to: http://www.projky.com/dotnet/4.5.1/System/String.cs.html

I have to give a thumbs up. This design is very clever .. The execution of programs in. NET is in the thread. Each thread has its own cache, so there is no need to worry about synchronization and locking. Moreover, the cache object size is limited.

Next, we will summarize its principles:The static thread provides the ability to store static fields independently in multiple threads, and then stores cached objects by operating on this static field.

Related Article

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.