public static SingletonThreadSafe GetInstancia(string value)
        {
            // Este condicional es necesario para evitar que los subprocesos tropiecen con el
            // bloquear una vez que la instancia esté lista.
            if (_instancia == null)
            {
                // Ahora, imagina que el programa acaba de lanzarse. Ya que
                // todavía no hay una instancia de Singleton, varios subprocesos pueden
                // pasar simultáneamente el condicional anterior y llegar a este
                // punto casi al mismo tiempo. El primero de ellos adquirirá
                // bloquear y continuará, mientras que el resto esperará aquí.
                lock (_bloqueo)
                {
                    // El primer hilo en adquirir el bloqueo, llega a este
                    // condicional, entra y crea la instancia del Singleton.
                    // Una vez que sale del bloque de bloqueo, un hilo que
                    // podría haber estado esperando la liberación de bloqueo puede entonces
                    // entrar en esta sección. Pero dado que el campo Singleton ya ha sid
                    // inicializado, el hilo no creará un nuevo objeto.
                    if (_instancia == null)
                    {
                        _instancia       = new SingletonThreadSafe();
                        _instancia.Value = value;
                    }
                }
            }

            return(_instancia);
        }
Beispiel #2
0
        public void TestThreadSafeSingleton()
        {
            SingletonThreadSafe threadSafe1 = SingletonThreadSafe.GetInstance();

            SingletonThreadSafe threadSafe2 = SingletonThreadSafe.GetInstance();

            Assert.AreSame(threadSafe1, threadSafe2);
        }
        private static void SingletonPattern()
        {
            SingletonClassic singletonClassic = SingletonClassic.getInstance();

            Console.WriteLine(singletonClassic.getDescription());

            SingletonThreadSafe singletonThreadSafe = SingletonThreadSafe.getInstance();

            Console.WriteLine(singletonThreadSafe.getDescription());
        }
Beispiel #4
0
 /// <summary>
 /// Gets the instance.
 /// </summary>
 /// <value>
 /// The instance.
 /// </value>
 public static SingletonThreadSafe Instance()
 {
     if (instance == null)
     {
         lock (syncRoot)
         {
             if (instance == null)
             {
                 instance = new SingletonThreadSafe();
             }
         }
     }
     return(instance);
 }
    public static SingletonThreadSafe getInstance()
    {
        if (instance == null)
        {
            lock (mutex)
            {
                if (instance == null)
                {
                    instance = new SingletonThreadSafe();
                }
            }
        }

        return(instance);
    }
 public static SingletonThreadSafe GetInstance(string value)
 {
     if (_instance == null)
     {
         lock (_lock)
         {
             if (_instance == null)
             {
                 _instance       = new SingletonThreadSafe();
                 _instance.Value = value;
             }
         }
     }
     return(_instance);
 }
Beispiel #7
0
        internal static void ExecuteSingleton()
        {
            //Intent
            //  • Ensure a class has only one instance, and provide a global point of access to it.
            //  • Encapsulated "just-in-time initialization" or "initialization on first use".

            //Problem
            //  Application needs one, and only one, instance of an object.
            //  Additionally, lazy initialization and global access are necessary.

            //NOTES
            //  In many ways, the Singleton Pattern is a convention for ensuring one and only one object is instantiated for a given class.
            //  Just ask yourself: how do I prevent more than one object from being instantiated? It’s not so obvious, is it?
            //  Let’s say you have an object that contains registry settings. You don’t want multiple copies of that object and its values running around
            //      – that would lead to chaos.By using an object you can assure that every object in your application is making use of the same global resource.

            //SingletonFirstVersion sfv = new SingletonFirstVersion();
            SingletonFirstVersion sfv2 = SingletonFirstVersion.Instance;

            //SingletonThreadSafe sts = new SingletonThreadSafe();
            SingletonThreadSafe sts2 = SingletonThreadSafe.Instance;
        }