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.