C # How does Singleton implement multi-threaded Multi-instance, each thread has only one instance

Source: Internet
Author: User

In the past, the singleton used in the project has always been the simplest C # method. It is Thread Safe, and only one instance can be used in multiple threads. The Code is as follows:
 
Public sealed class Singleton
{
Public static readonly Singleton Instance = new Singleton ();
Private Singleton (){}
}

Recently, the project has new requirements. Multiple instances need to be implemented in multiple threads. It is best to use a singleton in each thread. Therefore, the code is changed to the most basic implementation. This basic implementation method is widely used in almost all Chinese and English documents on the Internet. This method will open multiple instances under multiple threads, but the result... In any case, it will always be a single instance and cannot be understood. The Code is as follows. please correct me. The test environment is Windows 7 64bit, VS2010,. net framework 4.0.
Class Singleton:
1 public class Singleton: ICloneable
2 {
5 private static Singleton instance;
6 public static Singleton Instance
7 {
8 get
9 {
10 if (instance = null)
11 instance = new Singleton ();
12 return instance;
13
14}
15}
16
17 private string instanceID;
18
19 private Singleton ()
20 {
21 // return a random ID to verify whether the instance is the same.
22 instanceID = new Random (). Next (). ToString ();
23}
24
25 public void GetInstanceID ()
26 {
27 System. Threading. Thread. Sleep (3000 );
28 MessageBox. Show ("Instance ID:" + instanceID );
29}
30
31 public object Clone ()
32 {
33 return new Singleton ();
34}
35}

ICloneable is implemented for multiple instances in multiple threads, but it still does not work. The ICloneable interface is not implemented at the beginning, but not at the same time. Www.2cto.com
Call code in Form1:
1 private void button#click (object sender, EventArgs e)
2 {
3 Singleton. Instance. GetInstanceID ();
4 new System. Threading. Tasks. TaskFactory (). StartNew () =>
5 {
6 Singleton. Instance. GetInstanceID ();
7 });
8
9}

As a result, after the IstanceID is obtained under the current thread, the InstanceID in the multi-thread is always the same. It turns out that no new instances are generated in other threads, which is a problem with my implementation method, why? Please correct me.
The running result is as follows: (backgroudnworker and other methods have also been tested, and it is always the same instance)




From Tian Yu

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.