C #. Net Singleton standalone Mode

Source: Internet
Author: User

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!

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.