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