Part 95   Deadlock in a multithreaded program

class Program
{
static void Main(string[] args)
{
Console.WriteLine("main start");
Account a1 = new Account(,);
Account a2 = new Account(,);
AccountManager m1 = new AccountManager(a1,a2,);
Thread t1 = new Thread(m1.Transfer);
t1.Name = "t1";
AccountManager m2 = new AccountManager(a2, a1, );
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();
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);
}
}
} }

Part 96   How to resolve a deadlock in a multithreaded program

static void Main(string[] args)
{
Console.WriteLine("main start");
Account a1 = new Account(,);
Account a2 = new Account(,);
AccountManager m1 = new AccountManager(a1,a2,);
Thread t1 = new Thread(m1.Transfer);
t1.Name = "t1";
AccountManager m2 = new AccountManager(a2, a1, );
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();
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());
}
}
}

Part 95 to 96 Deadlock in a multithreaded program的更多相关文章

  1. Part 97 Performance of a multithreaded program

    class Program { static void Main(string[] args) { Stopwatch s = new Stopwatch(); s.Start(); EvenNumb ...

  2. 【转】Multithreaded Python Tutorial with the “Threadworms” Demo

    The code for this tutorial can be downloaded here: threadworms.py or from GitHub. This code works wi ...

  3. Introduction to Multi-Threaded, Multi-Core and Parallel Programming concepts

    https://katyscode.wordpress.com/2013/05/17/introduction-to-multi-threaded-multi-core-and-parallel-pr ...

  4. Timer.5 - Synchronising handlers in multithreaded programs

    This tutorial demonstrates the use of the boost::asio::strand class to synchronise callback handlers ...

  5. Classloaders and Classes

    Classloaders and Classes (CLASSES) An example of the classloader (CLASSES) section that includes Cla ...

  6. Java+Windows+ffmpeg实现视频转换

    最近由于项目需要,研究了一下如何用Java实现视频转换,“着实”废了点心思,整理整理,写出给自己备忘下. 思路 由于之前没有没法过相关功能的经验,一开始来真不知道从哪里入手.当然,这个解决,googl ...

  7. Java学习笔记(14)

    需求:一个银行账户5000块,两夫妻一个拿着存折,一个拿着卡,开始取钱比赛,每次只能取1000,要求不准出现线程安全问题 public class Demo10 { public static voi ...

  8. ProxySQL Tutorial : setup in a MySQL replication topology

    ProxySQL Tutorial : setup in a MySQL replication topology 时间 2015-09-15 05:23:20 ORACLE数据库技术文刊 原文  h ...

  9. [转载]C#深入分析委托与事件

    原文出处: 作者:风尘浪子 原文链接:http://www.cnblogs.com/leslies2/archive/2012/03/22/2389318.html 同类链接:http://www.c ...

随机推荐

  1. linux集群管理<转>

    云在根本上是由硬件和软件组成的,这些组件需要经常细心地维护.出现故障的硬件需要修理或更换:软件需要应用补丁.更新和升级:必须根据需求和潜在的安全威胁提前配置系统.应用程序开发人员可能觉得计算云很方便. ...

  2. 使用Python脚本进行域名解析

    因为在研究爬虫,所以也了解了下域名解析.要提高爬虫的效率,就需要提高域名解析的效率.我将爬虫记录下的域名作为待解析的域名来测试各域名解析方法的效率.我尝试以下四种方法:1. 单线程依次解析各域名,2. ...

  3. Educational Codeforces Round 7 A. Infinite Sequence 水题

    A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/622/problem/A Description Consider the ...

  4. Codeforces Round #336 (Div. 2) C. Chain Reaction set维护dp

    C. Chain Reaction 题目连接: http://www.codeforces.com/contest/608/problem/C Description There are n beac ...

  5. winForm 程序开发界面参数传递

    1. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; u ...

  6. 【JS】defer / async

    引用JavaScript文件时的两个属性defer和async <script src="js1.js" defer></script><script ...

  7. MySql之char与varchar

    MySql之char与varchar的差别 char是一种固定长度的类型,varchar则是一种可变长度的类型.它们的差别是:  1. char(M)类型的数据列里.每一个值都占用M个字节.假设某个长 ...

  8. 暂时解决Sublime Text 2不支持input问题(转)

    (1)打开当前python文件 (2)然后 Tools -> Command Palette (3)SublimeREPL Python RUN current file (4)就会打开新窗口, ...

  9. MYSQL: Handler_read_%参数说明

      环境: 表t_feed_idx(user_id bigint, feed_id bigint, KEY (`user_id`,`feed_id`)) engine=innodb;表t_feed_i ...

  10. js json与对象的相互转换

    var str = '{ "name": "cxh", "sex": "man" }'; //JSON字符串:var o ...