The life cycle and thread safety of a static variable for ASP.

Source: Internet
Author: User

  void Application_Start Start

  void Application_End ended, which was right.

Today to do a global application, want to confirm, on the internet to find, my God, said anything has

About three kinds of

1.application_start--application_end

2.session_start--session_end

3. End of class life cycle

I made a test with 4 machines. The static variable value is always constant, and is not destroyed because other users are logged in, the confirmation should be application level

A static class invokes the static constructor to create a class type object on first access, and the lifetime of the type object is the lifetime of the entire application domain, as well as the static class being accessed, which is unloaded only if the application domain it resides in is unloaded.

The members of a class are divided into two classes, static members (static member), and instance members (instance member). A static member belongs to a class and an instance member is an instance of an object, that is, a class.

A brief discussion of whether static fields and static methods are used in a class is a thread-safe issue.

We are aware that the call to static field and static method is called through the class. Static methods do not operate on specific instances, only static members. Instance methods can operate on specific instances, access static members, and access instance members.

So, is the static method used in multi-threading a thread-safe issue? This depends on whether static methods are used in static methods to see if static members are employed.

Because, when using the same static method in multi-threading, each thread uses a copy of its own instance field (instance field) and shares a static field. So, if the static method does not manipulate a static member, only the instance fields (instance field) are used inside the method, which does not cause security problems. However, if the static method operates on a static field, it needs to be handled securely in a static method with mutually exclusive access.

For a simple example, we use the Console.WriteLine (); WriteLine () is a static method of the Console.WriteLine class.

For ASP. NET, multiple client Access server side, this is a multithreaded example. As long as we understand the reason, we can safely use static methods in the data access layer in the three-tier architecture to access the database.

  Static methods There is no thread-safety issue if static variables are not used.

Why is it? Because of variables declared within a static method, each thread is called to create a new copy without sharing a single storage unit. Each thread will create its own copy, so it will not be wired for security issues

Note that static variables, because they occupy a store during class loading, each thread is shared with this store, so if static variables are used in static methods, this will be a security issue for threads!

The life cycle and thread safety of a static variable for ASP.

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.