Пример #1
0
        private void simulate(SingletonType singletonType)
        {
            for (int i = 0; i < 10; i++)
            {
                Task.Run(() =>
                {
                    for (int j = 0; j < 30; j++)
                    {
                        // var amt = this.getRandomAmt(); //預購數
                        var amt = 2; //預購數

                        NumberProvider counter = null;

                        switch (singletonType)
                        {
                        case SingletonType.NonThreadSafe:
                            counter = NonThreadSafeSingleton.Instance;
                            break;

                        case SingletonType.DoubleCheck:
                            counter = DoubleCheckSingleton.Instance;
                            break;

                        case SingletonType.Eager:
                            counter = EagerSingleton.Instance;
                            break;

                        case SingletonType.Lazy:
                            counter = LazySingleton.Instance;
                            break;
                        }

                        string preorderNumbers = string.Empty; //預購序號集合
                        for (int x = 0; x < amt; x++)
                        {
                            var number       = counter.GetNumber();
                            preorderNumbers += $"*{number}* ";
                        }

                        Trace.WriteLine($"({Thread.CurrentThread.ManagedThreadId}){this.getRandomName()}預購{amt}組: {preorderNumbers}");
                    }
                });
            }
        }
Пример #2
0
 public void DoSomething()
 {
     Console.WriteLine(_numberProvider.GetNumber());
 }