Thread Safe Singleton Design Pattern in C#

This article going to explain on thread-safe singleton design pattern by using parallel.invoke the method in the main class and the object class and lock class in the singleton class.

Thread safe singletonton design pattern dot net

parallel.invoke is used to invokes methods as action. and pass the methods in parallel.invoke.

actually, we can achieve the singleton design pattern without parallel class or lock class inside the singleton design pattern but the problem is the thread-safe. if we don't take care of thread-safe then there is no use if we implement the singleton. for example, parallel to get multi-thread concept.

Lazy initialization works well in a single-threaded scenario. but not the well multi-threaded scenario.


Main Class :

in main class use, the invoke method of parallel class and declare the methods as a lambda expression as shown below code. pass the required methods.

             Parallel.Invoke(

                        ()=> Customer(),

                        ()=> Vendor()

                    );


      using System;

using System.Threading.Tasks;

 

namespace ConsolePractice

{

    class Program

    {

        static void Main(string[] args)

        {

            Parallel.Invoke(

                   ()=> Customer(),

                   ()=> Vendor()

                );

           

            Console.ReadLine();

        }

 

        private static void Vendor()

        {

        Singleton_Design_Pattern obj1 = Singleton_Design_Pattern.GetNewInstance;

            obj1.getEmployee("Vendor Name Tata");

        }

         private static Singleton_Design_Pattern Customer()

        {

         Singleton_Design_Pattern obj = Singleton_Design_Pattern.GetNewInstance;

            obj.getEmployee("Employee Name Murali Krishna");

            return obj;

        }

    }

}




Thread Safe Singleton design pattern.

initialize the object class and initialize a new object. use the lock class for multi-thread safe with that object reference.


using System;

using System.Threading.Tasks;

 

namespace ConsolePractice

{

   public sealed class Singleton_Design_Pattern

    {

        private static int count =0;

        private static Singleton_Design_Pattern singletonInstance = null;

        private static readonly object objctlock = new object();

        public static Singleton_Design_Pattern GetNewInstance

        {

            get {

                lock (objctlock)

                {

                    if (singletonInstance == null)

                    {

                        singletonInstance = new Singleton_Design_Pattern();

                    }

 

                }

              

                return singletonInstance;

            }

        }

 

        private Singleton_Design_Pattern()

        {

            count++;

            Console.WriteLine("object "+count);

        }

        public void getEmployee(string message)

        {

            Console.WriteLine(message);

        }

    }

}



OutPut Thread Safe Singleton Design Pattern in C#


Thread Safe Singleton Design Pattern out put
the output shows only one instance creation.

Double checked locking approach for Thread-safe Singleton Design Pattern in C#

in this approach, we are going to check whether the instance is created or not.


     using System;

using System.Threading.Tasks;

 

namespace ConsolePractice

{

   public sealed class Singleton_Design_Pattern

    {

        private static int count =0;

        private static Singleton_Design_Pattern singletonInstance = null;

        private static readonly object objctlock = new object();

        public static Singleton_Design_Pattern GetNewInstance

        {

            get

            {

 

                if (singletonInstance == null)

                {

                    lock (objctlock)

                    {

                        if (singletonInstance == null)

                        {

                            singletonInstance = new Singleton_Design_Pattern();

                        }

                    }

                }

 

                return singletonInstance;

            }

        }

 

        private Singleton_Design_Pattern()

        {

            count++;

            Console.WriteLine("object "+count);

        }

        public void getEmployee(string message)

        {

            Console.WriteLine(message);

        }

    }

}

             


Conclusion

we achieved the multithread safe by using object class and lock class at singleton class in singleton design pattern with thread safe. and  Double-checked locking approach for Thread-safe Singleton Design Pattern in C# because the locks are very expensive to use each and every time to avoid that problem we are going to check the null instance before going to lock check.




Previous Post Next Post