Ejemplo n.º 1
0
        }     // RunBoiler

        static void CheckBoiler(ChocolateBoiler boiler)
        {
            while (true)
            {
                int t = boiler.GetTemperature();
                WriteLine("Temperature of boiler <{0}> is {1}°", boiler.Name, t);
                Thread.Sleep(500);
            } // forever
        }     // RunBoiler
Ejemplo n.º 2
0
        static void RunBoiler(ChocolateBoiler boiler)
        {
            while (true)
            {
                boiler.Fill();
                Thread.Sleep(1000);

                boiler.Boil();
                Thread.Sleep(3000);

                boiler.Drain();
                Thread.Sleep(1000);
            } // forever
        }     // RunBoiler
Ejemplo n.º 3
0
        }     // RunBoiler

        static void Main(string[] args)
        {
            ChocolateBoiler b;

            // Create the one and only boiler instance:
            b = ChocolateBoiler.GetInstance("Hershey-1");

            // Create and start temperature monitoring thread:
            Thread t1 = new Thread(() => CheckBoiler(b));

            t1.Start();

            // Create and start process controlling thread (on boiler singleton):
            b = ChocolateBoiler.GetInstance();
            Thread t2 = new Thread(() => RunBoiler(b));

            t2.Start();

            // Keep console open:
            ReadLine();
        } // Main.
        public static ChocolateBoiler GetInstance(string name)
        {
            // use double-checked locking to ensure thread safety:
            if (uniqueInstance == null)
            {
                Object lockThis = new Object();
                lock ( lockThis )
                {
                    uniqueInstance = new ChocolateBoiler();

                    if (name != null)
                    {
                        uniqueInstance.name = name;
                    }

                    uniqueInstance.IsEmpty = true;
                } // lock
            }     // first instance

            return(uniqueInstance);
        } // ctor