Singleton mode MVC

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

Namespace Singleleton
{
public class Singleleton
{
Declare a static variable to store the entity
3. To save an instance of the created instance that defines a static variable to save
private static Singleleton _instance = null;

<summary>
4. Create an object for the yoke to prevent multiple instances of multithreading creation
</summary>
private static Object locker = new Object ();

<summary>
1. Private constructor, eliminate the user directly new to get the object instance (to prevent the user casually new for this topic singleton mode, otherwise it is not a singleton may be accessible)
</summary>
Private Singleleton ()
{

}

<summary>
2. Defines a static public method to get the instance object
Provides an instance access portal
</summary>
<returns></returns>
public static Singleleton Getintence ()
{
If _instance==null is not locked
if (_instance = = null)
{


When the first thread comes in, it locks up the area, and the other threads don't get in, and other threads can enter after the current thread has finished.
Lock (Locker)
{
if (_instance = = null)
{
Because the same class can access the private one, you can new
_instance = new Singleleton ();
}
}
}
return _instance;
}
}
}

Singleton mode MVC

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.