星空网 > 软件开发 > ASP.net

Part 95 to 96 Deadlock in a multithreaded program

Part 95   Deadlock in a multithreaded program

Part 95 to 96   Deadlock in a multithreaded program

Part 95 to 96   Deadlock in a multithreaded programPart 95 to 96   Deadlock in a multithreaded program
class Program  {    static void Main(string[] args)    {      Console.WriteLine("main start");      Account a1 = new Account(001,10000);      Account a2 = new Account(002,20000);      AccountManager m1 = new AccountManager(a1,a2,5000);      Thread t1 = new Thread(m1.Transfer);      t1.Name = "t1";      AccountManager m2 = new AccountManager(a2, a1, 3000);      Thread t2 = new Thread(m2.Transfer);      t2.Name = "t2";      t1.Start();      t2.Start();      t1.Join();      t2.Join();      Console.WriteLine("main end");    }  }  class Account  {    public int ID { get; set; }    public double Balance { get; set; }    public Account(int id, double balance)    {      this.ID = id;      this.Balance = balance;    }    public void WithDraw(double amount)    {      Balance -= amount;    }    public void Deposit(double amount)    {      Balance += amount;    }  }  class AccountManager  {    public Account FromAccount { get; set; }    public Account ToAccount { get; set; }    public double AmountToTransfer { get; set; }    public AccountManager(Account from,Account to,double amountToTransfer)    {      this.FromAccount = from;      this.ToAccount = to;      this.AmountToTransfer = amountToTransfer;    }    public void Transfer()    {      Console.WriteLine(Thread.CurrentThread.Name+"try to acquire lock on"+FromAccount.ID.ToString());      lock (FromAccount)      {        Console.WriteLine(Thread.CurrentThread.Name+" acquired lock on "+FromAccount.ID.ToString());        Console.WriteLine(Thread.CurrentThread.Name+" suspended for 1 second");        Thread.Sleep(1000);        Console.WriteLine(Thread.CurrentThread.Name+"back in action and try to acquire lock on" +ToAccount.ID.ToString());        lock (ToAccount)        {          Console.WriteLine("this code will not execute");          FromAccount.WithDraw(AmountToTransfer);          ToAccount.Deposit(AmountToTransfer);        }      }    }  }

View Code

Part 95 to 96   Deadlock in a multithreaded program

Part 96   How to resolve a deadlock in a multithreaded program

Part 95 to 96   Deadlock in a multithreaded program

Part 95 to 96   Deadlock in a multithreaded programPart 95 to 96   Deadlock in a multithreaded program
static void Main(string[] args)    {      Console.WriteLine("main start");      Account a1 = new Account(101,10000);      Account a2 = new Account(102,20000);      AccountManager m1 = new AccountManager(a1,a2,5000);      Thread t1 = new Thread(m1.Transfer);      t1.Name = "t1";      AccountManager m2 = new AccountManager(a2, a1, 3000);      Thread t2 = new Thread(m2.Transfer);      t2.Name = "t2";      t1.Start();      t2.Start();      t1.Join();      t2.Join();      Console.WriteLine("main end");    }  }  class Account  {    public int ID { get; set; }    public double Balance { get; set; }    public Account(int id, double balance)    {      this.ID = id;      this.Balance = balance;    }    public void WithDraw(double amount)    {      Balance -= amount;    }    public void Deposit(double amount)    {      Balance += amount;    }  }  class AccountManager  {    public Account FromAccount { get; set; }    public Account ToAccount { get; set; }    public double AmountToTransfer { get; set; }    public AccountManager(Account from,Account to,double amountToTransfer)    {      this.FromAccount = from;      this.ToAccount = to;      this.AmountToTransfer = amountToTransfer;    }    public void Transfer()    {      object _lock1, _lock2;      if(FromAccount.ID<ToAccount.ID)      {        _lock1 = FromAccount;        _lock2 = ToAccount;      }      else      {        _lock1 = ToAccount;        _lock2 = FromAccount;      }      Console.WriteLine(Thread.CurrentThread.Name+"try to acquire lock on "+((Account)_lock1).ID.ToString());      lock (_lock1)      {        Console.WriteLine(Thread.CurrentThread.Name + " acquired lock on " + ((Account)_lock1).ID.ToString());        Console.WriteLine(Thread.CurrentThread.Name+" suspended for 1 second");        Thread.Sleep(1000);        Console.WriteLine(Thread.CurrentThread.Name + "back in action and try to acquire lock on " + ((Account)_lock2).ID.ToString());        lock (_lock2)        {          Console.WriteLine(Thread.CurrentThread.Name + " acquired lock on " + ((Account)_lock2).ID.ToString());          FromAccount.WithDraw(AmountToTransfer);          ToAccount.Deposit(AmountToTransfer);          Console.WriteLine(Thread.CurrentThread.Name+" Transferd "+AmountToTransfer.ToString()+" from "+FromAccount.ID.ToString()+" to "+ToAccount.ID.ToString());        }      }    }

View Code

Part 95 to 96   Deadlock in a multithreaded program




原标题:Part 95 to 96 Deadlock in a multithreaded program

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

厉害的亚马逊运营到底厉害在那里?:https://www.kjdsnews.com/a/552719.html
“疫”外之财,缘何有人欢喜有人忧愁?:https://www.kjdsnews.com/a/553660.html
你还在做无效且价格高的亚马逊PPC优化???:https://www.kjdsnews.com/a/553661.html
在产品面前,品牌算个P:https://www.kjdsnews.com/a/553662.html
TikTok转化极低!DTC却很火爆!:https://www.kjdsnews.com/a/553663.html
【亚马逊】大卖小卖是如何测款的:https://www.kjdsnews.com/a/553664.html
秦皇岛到丹东旅游景点大全 秦皇岛到丹东开车多长时间:https://www.vstour.cn/a/408253.html
求推荐适合情侣玩的地方,三天时间,谢谢:https://www.vstour.cn/a/408254.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流