Visual C#: MultiThreadSingleton

Visual C#: MultiThreadSingleton

This article demonstrates how to make multi threaded singleton

01.public sealed class MultiThreadSingleton<T> where T : class
02.    {
03.        #region Private Fields
04.        /// <summary>
05.        ///
06.        /// </summary>
07.        private static volatile T instance;
08. 
09.        /// <summary>
10.        ///  sync lock for creating
11.        /// </summary>
12.        private static object synclock = new object();
13. 
14.        #endregion
15. 
16.        #region Constructor
17.        /// <summary>
18.        /// Initializes QMultiThreadSingleton
19.        /// </summary>
20.        private MultiThreadSingleton()
21.        {
22.        }
23. 
24.        #endregion
25. 
26.        /// <summary>
27.        ///
28.        /// </summary>
29.        public static T Instance
30.        {
31.            get
32.            {
33.                if (instance == null)
34.                {
35.                    lock (synclock)
36.                    {
37.                        if (instance == null)
38.                        {
39.                           var type = typeof(T);
40.                           instance = Activator.CreateInstance(type, true) as T;                           
41.                        }
42.                    }
43.                }
44.                return instance;
45.            }
46.        }
47. 
48.    }

now  check that singleton in  multiple threads

01.class Program
02.    {
03.        static void Main(string[] args)
04.        {
05.            DemoClassOne test = MultiThreadSingleton<DemoClassOne>.Instance;
06.            test.SampleInetgerProperty = 99;
07.            test.DemoPropertyOne = "No Operation";
08. 
09.            Parallel.For(0, 5, i => {
10.                ThreadTest threadObject = new ThreadTest(i);
11.                DemoClassOne singletonObj = MultiThreadSingleton<DemoClassOne>.Instance;
12.                threadObject.MyProperty = singletonObj;
13.                Parallel.For(0, 2, j => {
14.                    if (j == 0)
15.                    {
16.                        lock (threadObject)
17.                        {
18.                            threadObject.OperationOne();
19.                        }
20.                    }
21.                    else
22.                    {
23.                        lock (threadObject)
24.                        {
25.                            threadObject.OperationTwo();
26.                        }
27.                    }
28.                });
29. 
30.                
31.            });
32. 
33.            var newobject = MultiThreadSingleton<DemoClassOne>.Instance;
34. 
35.            Console.WriteLine("Thread {0} ,Final , MyProperty.CheckBoolProoperty {1} , MyProperty.SampleInetgerProperty {2} ,MyProperty.DemoPropertyOne {3}",
36.               System.Threading.Thread.CurrentThread.ManagedThreadId, newobject.CheckBoolProoperty, newobject.SampleInetgerProperty, newobject.DemoPropertyOne);           
37.            Console.ReadLine();
38. 
39.        }       
40.    }
41. 
42. 
43.    public class ThreadTest
44.    {
45. 
46.        public DemoClassOne MyProperty { get; set; }
47. 
48.        private int ThreadId { get; set; }
49. 
50.        private int randomNumber = 0;
51. 
52.        public ThreadTest(int threadId)
53.        {
54.            this.ThreadId = threadId;
55.            this.randomNumber = new Random().Next(2, 12);
56.             
57.        }
58. 
59.        public void OperationOne()
60.        {
61.            Console.WriteLine("Thread {0} ,OperationOne Start , MyProperty.CheckBoolProoperty {1} , MyProperty.SampleInetgerProperty {2} ,MyProperty.DemoPropertyOne {3}",
62.                this.ThreadId, this.MyProperty.CheckBoolProoperty, this.MyProperty.SampleInetgerProperty, this.MyProperty.DemoPropertyOne);
63.            if (!this.MyProperty.CheckBoolProoperty)           
64.            {
65.                this.MyProperty.DemoPropertyOne = "Operation 1";
66.                this.MyProperty.SampleInetgerProperty = this.ThreadId * 3;
67.                this.MyProperty.CheckBoolProoperty = !this.MyProperty.CheckBoolProoperty;
68. 
69.            }
70.            Console.WriteLine("Thread {0} ,OperationOne End , MyProperty.CheckBoolProoperty {1} , MyProperty.SampleInetgerProperty {2} ,MyProperty.DemoPropertyOne {3}",
71.                this.ThreadId, this.MyProperty.CheckBoolProoperty, this.MyProperty.SampleInetgerProperty, this.MyProperty.DemoPropertyOne);
72. 
73. 
74.        }
75. 
76.        public void OperationTwo()
77.        {
78.            Console.WriteLine("Thread {0} ,OperationTwo Start , MyProperty.CheckBoolProoperty {1} , MyProperty.SampleInetgerProperty {2} ,MyProperty.DemoPropertyOne {3}",
79.                this.ThreadId, this.MyProperty.CheckBoolProoperty, this.MyProperty.SampleInetgerProperty, this.MyProperty.DemoPropertyOne);
80.            if (this.MyProperty.CheckBoolProoperty)
81.            {
82.                this.MyProperty.DemoPropertyOne = "Operation 2";
83.                this.MyProperty.SampleInetgerProperty = this.ThreadId * 3;
84.                this.MyProperty.CheckBoolProoperty = !this.MyProperty.CheckBoolProoperty;
85. 
86.            }
87.            Console.WriteLine("Thread {0} ,OperationTwo End , MyProperty.CheckBoolProoperty {1} , MyProperty.SampleInetgerProperty {2} ,MyProperty.DemoPropertyOne {3}",
88.                this.ThreadId, this.MyProperty.CheckBoolProoperty, this.MyProperty.SampleInetgerProperty, this.MyProperty.DemoPropertyOne);
89.        }
90. 
91.         
92.         
93.    }

Leave a Comment
  • Please add 1 and 5 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Naomi  N edited Revision 1. Comment: Article needs more work

Page 1 of 1 (1 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Naomi  N edited Revision 1. Comment: Article needs more work

  • Can you explain what this code is doing, and when/why you would want to do it? Is this for the August C# Guru contest? If so, is it added to that page yet?

    Thanks!

Page 1 of 1 (2 items)