. Net Static and Singleton,. NetStatic

Source: Internet
Author: User

. Net Static and Singleton,. NetStatic

Static keywords can be used as modifiers for classes, methods, and member variables. It means that the objects, methods, and variables that access the modifier are referenced to the same instance (memory address) throughout the application lifecycle ). However, thread security problems may occur in multiple threads: The count field of the counter is static, and the 1000 Increase method is called cyclically in multiple threads. the result may not be 1000.

internal class Counter    {        public static int count = 0;        public static void Increase()        {            Thread.Sleep(10);            Counter.count++;        }    }

 

To solve this problem, you must first understand the Static features:

1. If you modify a variable, even if it is a value type, such as an Int variable, the variable is the same instance (memory address) no matter where it is used ). In this case, it shows the feature of reference type. The static variable is called in the method, instead of putting the value type into the stack, but a pointer to the variable.

2. the memory allocation of static classes, variables, and methods is determined at application compilation and allocated at application initialization, the application is destroyed only when its lifecycle ends. And they are neither stored on the stack nor on the stack,Static Data Zone. In this way, the whole application can access the same instance globally. Here is an article describing the details of a static storage area: http://www.educity.cn/zk/bianyi/201307031614161496.htm

3. Static classes cannot be inherited or instantiated. They cannot have common constructors, but can have static structures to initialize static members. Static classes can only have static members or methods.

4. Static constructor can be used in both static classes and common classes. It serves to provide a suitable time for initializing static members. Static members in a common class are initialized when the application starts. At this time, even the main function is not executed, normal constructors cannot be called. For a class with a static constructor, the initialization of its static member will be postponed until any static member or static method in the class is actually called before the default Initialization is triggered, the static constructor is triggered.Static structures can only be called once, and only static members can be initialized.If this class is directly initialized, the static structure is triggered first, and then the common constructor is triggered.

It can be seen that the most fundamental reason is that two threads simultaneously access static resources at the same time, and the resource status is modified at the same time. Therefore, local serialization of shared resources is required, that is, thread-safe Singleton.

Internal class LockCounter {public static int count = 0; public static void Increase () {Thread. sleep (10); lock (typeof (LockCounter) {LockCounter. count ++ ;}}}View Code

Furthermore, InterLocked is used for atomic operations instead of Lock.

Internal class InterLockCounter {public static int count = 0; public static void Increase () {Thread. Sleep (10); Interlocked. Increment (ref InterLockCounter. count );}}View Code

However, if we use this feature of static construction, we can use it to implement the simple and lockless Singleton mode, and it is thread-safe, any thread trying to access the instance will trigger the static structure to initialize the object instance before access, and the static structure can only be called once, without the problem of multithreading.

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.