Пример #1
0
        public void ConcurrentDebitCredit()
        {
            var a = new Account();

            Parallel.Invoke(() => a.Credit(100),
                            () => a.Debit(50));

            Assert.AreEqual(50, a.CurrentBalance);
        }
Пример #2
0
        public static bool Run()
        {
            var a = new Account();

            Parallel.Invoke(() => a.Credit(100),
                            () => a.Debit(50));

            Console.WriteLine(a.CurrentBalance == 50 ? "Pass" : "Fail");

            return a.CurrentBalance == 50;
        }
Пример #3
0
        public void ConcurrentDebitCredit_TheOldWay()
        {
            for (var i = 0; i < 1000; i++)
            {
                var a = new Account();

                Parallel.Invoke(() => a.Credit(100),
                                () => a.Debit(50));

                Assert.AreEqual(50, a.CurrentBalance);
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("usage: TestApp iterations");
                return;
            }

            for (var i = 0; i < int.Parse(args[0]); i++)
            {
                var a = new Account();

                Parallel.Invoke(() => a.Credit(100),
                                () => a.Debit(50));

                Console.WriteLine(a.CurrentBalance == 50 ? "Pass {0}" : "Fail {0}", i);

                if (a.CurrentBalance != 50)
                {
                    return;
                }
            }
            Console.WriteLine("Passed having run {0} iterations", args[0]);
        }