Singleton is an object that is often generated to ensure that the application operates on a global object and keeps it consistent, such as locking the read/write operations on the file, transaction rollback and Task Manager operations during database operations are read in a single mode. To create a single mode class, three conditions must be met: 1: private constructor (to prevent other objects from creating instances); 2: private variable of a single type; 3: under the static global Acquisition Interface, I write a class. to check whether it is a single unit, a counter is added. If it is the same class, the Count of this class should be automatically added after each call, instead of resetting the object to zero :. net C # standalone Mode
using System; using System.Threading;
public class Singleton
{
private int ObjCount=0;
private Singleton()
{Console. writeline ("create object ");}
private static Singleton objInstance = null;
public static Singleton getInstance()
{
if (objInstance==null) objInstance=new Singleton();
return objInstance;
}
public void ShowCount()
{ O
bjCount++;
Console. writeline ("A single object has been called {0} Times", objcount );
}
}; Then let's test: public class consoletest {public static void main (string [] ARGs) {console. writeline ("start execution in standalone mode"); For (INT I = 0; I <5; I ++) {Singleton. getinstance (). showcount ();} console. readline ();}};
I executed five times in this main to see the output result: start to execute the standalone mode to create an object. A single object is called once. A single object is called twice. A single object is called three times. A single object is called four times. A single object is called five times. here we can see that, each time, the same object is used to implement the monomer. To test whether it is a single component under multiple threads, I wrote a multi-threaded test: Class apartmenttest {public static void runmorethread () {thread newthread = new thread (New threadstart (threadsinglemethod )); newthread. setapartmentstate (apartmentstate. MTA); console. writeline ("threadstate: {0}, apartmentstate: {1}, managedthreadid: {2}", newthread. threadstate, newthread. getapartmentstate (), newthread. managedthreadid); newthread. start ();} public static void threadsinglemethod () {Singleton. getinstance (). showcount ();}};
Then apartmenttest is executed every for loop. runmorethread (); then let's look at the output result: Start to execute the standalone mode threadstate: unstarted, apartmentstate: MTA, managedthreadid: 3 create an object. A single object is called once threadstate: unstarted, apartmentstate: MTA, managedthreadid: 4 a single object is called twice threadstate: unstarted, apartmentstate: MTA, managedthreadid: 5 a single object is called three times threadstate: unstarted, apartmentstate: MTA, managedthreadid: 6. A single object is called for four times. threadstate: unstarted, apartmentstate: MTA, Managedthreadid: 7 a single object has been called five times. According to the managedthreadid, we can see that different lines have reached the monomer access level. OK!