Part 95 to 96 Deadlock in a multithreaded program
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的更多相关文章
- Part 97 Performance of a multithreaded program
class Program { static void Main(string[] args) { Stopwatch s = new Stopwatch(); s.Start(); EvenNumb ...
- 【转】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 ...
- 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 ...
- Timer.5 - Synchronising handlers in multithreaded programs
This tutorial demonstrates the use of the boost::asio::strand class to synchronise callback handlers ...
- Classloaders and Classes
Classloaders and Classes (CLASSES) An example of the classloader (CLASSES) section that includes Cla ...
- Java+Windows+ffmpeg实现视频转换
最近由于项目需要,研究了一下如何用Java实现视频转换,“着实”废了点心思,整理整理,写出给自己备忘下. 思路 由于之前没有没法过相关功能的经验,一开始来真不知道从哪里入手.当然,这个解决,googl ...
- Java学习笔记(14)
需求:一个银行账户5000块,两夫妻一个拿着存折,一个拿着卡,开始取钱比赛,每次只能取1000,要求不准出现线程安全问题 public class Demo10 { public static voi ...
- ProxySQL Tutorial : setup in a MySQL replication topology
ProxySQL Tutorial : setup in a MySQL replication topology 时间 2015-09-15 05:23:20 ORACLE数据库技术文刊 原文 h ...
- [转载]C#深入分析委托与事件
原文出处: 作者:风尘浪子 原文链接:http://www.cnblogs.com/leslies2/archive/2012/03/22/2389318.html 同类链接:http://www.c ...
随机推荐
- 使用UILocalizedIndexedCollation实现区域索引排序 及 不显示没有数据的区域
UILocalizedIndexedCollation可以实现区域排序,类似通讯录的样式. //首先进行初始化 locationCollation = [UILocalizedIndexedColla ...
- 【java开发系列】— JDOM创建、改动、删除、读取XML文件
有非常多中操作XML文件的方法,这里介绍一下JDOM的用法和技巧. JDOM下载地址 创建XML文档 XML文件是一种典型的树形文件,每一个文档元素都是一个document元素的子节点. 而每一个子元 ...
- nginx-1.4.4 + tcp_proxy_module手动编译安装
Nginx开源软件默认没有提供TCP协议的负载均衡,下面记录一下我的安装过程: 1. 下载nginx最新稳定版的源码 mkdir /software cd /software yum install ...
- jQuery Mobile 手动显示ajax加载器,提示加载中...
在使用jQuery Mobile开发时,有时候我们需要在请求ajax期间,显示加载提示框(例如:一个旋转图片+一个提示:加载中...).这个时候,我们可以手动显示jQuery Mobile的加载器,大 ...
- 网站性能扩展案例:每天30-50亿请求,300K QPS是如何炼成的
Reduce Data广告服务网站(http://reducedata.com)如何扩展到每天300K QPS请求?分享经验如下: 1. 为大规模而设计,广告服务平台从一开始增长就很惊人,因此,系统开 ...
- 苹果iOS手机暗藏间谍软件的揭秘者:扎徳尔斯基
大家知道,苹果iOS手机的短消息server(SMS)是用硬件加密的,看起来非常安全.可是,Jonathan Zdziarski发现苹果公司有意地放进去一个"文件转发server" ...
- Oracle DB 管理数据库的空间
• 描述4 KB 扇区磁盘的概念及使用 • 使用可移动表空间 • 描述可移动表空间的概念 数据库存储 数据库存储 数据库包括物理结构和逻辑结构.由于物理结构和逻辑结构是分开的,因此管理数据的物 理存储 ...
- [MEAN Stack] First API -- 3. Select by ID with Mongoose and Express
Mongoose allows you to easily select resources by ID from your MongoDB. This is an important aspect ...
- [Node.js] Configuring npm package.json scripts
With a node package manager's (npm) package.json script property, you can preconfigure common tasks ...
- IOS编程之相机和相册
概述 IOS设备中的相机和相册,是我们在项目开发中经常会使用到的多媒体元素,使用相机可以获得最新想要的照片,而使用相册则可以访问IOS设备中的图片资源 使用IOS设备中的相机/相册获得图片资源 是否允 ...