Esempio n. 1
0
        public void TransferToCard(decimal amount, BankCard2 recipient)
        {
            bool lockTaken = false;

            try
            {
                Monitor.TryEnter(_sync, TimeSpan.FromSeconds(1), ref lockTaken);
                VerifyCard(recipient);
                _moneyAmount -= amount;
                recipient.ReceivePayment(amount);
            }
            finally
            {
                if (lockTaken)
                {
                    Monitor.Exit(_sync);
                }
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var bankcard  = new BankCard2(10000); //10000+500+300-500 = 10300
            var bankcard2 = new BankCard2(200);   //200+300+500 = 1000


            var t1 = Task.Factory.StartNew(() => { bankcard.ReceivePayment(500); });
            var t2 = Task.Factory.StartNew(() => {
                bankcard.TransferToCard(500, bankcard2);
                bankcard.ReceivePayment(300);
                bankcard2.ReceivePayment(300);
            });

            Task.WaitAll(new[] { t1, t2 });


            Console.WriteLine($"Card 1 amount is {bankcard.MoneyAmount}, Card 2 amount is {bankcard2.MoneyAmount}");


            /////////////////////////////////////////////////////////////////
            //Character c = new Character() { Name="hamada"};
            //Character c2 = new Character() { Name="mayada"};

            //Console.WriteLine($"c name is {c.Name} and c2 name is {c2.Name}");

            //var cref = Interlocked.Exchange(ref c, c2);
            //Interlocked.Exchange(ref c2, cref);

            //Console.WriteLine($"c name is {c.Name} and c2 name is {c2.Name}");


            //Console.WriteLine($"characheter health is {c.Health}");

            //var tasks = new List<Task>();

            //for (int i = 0; i < 100; i++)
            //{
            //    Task t1 = Task.Factory.StartNew(() => {
            //        for (int j = 0; j < 100; j++)
            //        {
            //            c.Hit(10);
            //        }
            //    });

            //    tasks.Add(t1);

            //    Task t2 = Task.Factory.StartNew(() => {
            //        for (int j = 0; j < 10; j++)
            //        {
            //            c.Heal(10);
            //        }
            //    });

            //    tasks.Add(t2);
            //}

            //Task.WaitAll(tasks.ToArray());

            //Console.WriteLine($"characheter health is {c.Armor}");



            ///////////////////////////////////////////////////////////////////////////////
            //Task.Run((Action)Do);

            //// Wait until we're fairly sure the other thread // has grabbed firstLock
            //Thread.Sleep(500);
            //Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}-Locking secondLock");

            //lock (secondLock)
            //{
            //    Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}-Locked secondLock");
            //    Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}-Locking firstLock");

            //    lock (firstLock)
            //    {
            //        Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}-Locked firstLock");
            //    }
            //    Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}-Released firstLock");
            //}
            //Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}-Released secondLock");

            Console.Read();
        }
Esempio n. 3
0
 private bool VerifyCard(BankCard2 card)
 {
     Thread.Sleep(5000);
     return(true);
 }