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. 关于Collection'

    ---恢复内容开始--- 众所周知在我们使用Java中的类的存储的时候经常会使用一些容器,链表的概念,本文将彻底帮您弄清链表的各种概念和模型!!!!注 意理解哦~~~ 大致框架如下: ----> ...

  2. 安装mysql问题

    我想大多数人都遇到第一次安装失败或者卸载安装mysql,老是在最后一步失败:解决方法有2个:1 删除 默认路径文件C:\Documents and Settings\All Users\Applica ...

  3. 使用ajax和window.history.pushState无刷新改变页面内容和地址栏URL

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. [Express] Level 1: First Step

    Installing Express Let's start building our new Express application by installing Express. Type the ...

  5. MySql文章

    转:  MySql安全建议    http://www.cnblogs.com/crystal189/p/3492640.html

  6. 微软发布屏蔽Win10升级的官方办法

    微软发布屏蔽Win10升级的官方办法 导读 微软似乎从来就没有像现在这么焦燥过,当然了,攸关生死,他要还是能"蛋"定得住,那才真叫怪了.你看,为了推广Windows 10,一向傲娇 ...

  7. 使用jmeter对ActiveMQ集群性能方案进行评估--转载

    原文地址:http://www.51testing.com/html/78/23978-143163.html 1.测试概要1.1 关于这篇文档中涉及的基于JMS的消息系统能为应用程序提供可靠的,高性 ...

  8. 功能分解——Android下画分时图与k线图有感

    最近工作极度繁忙,已经好久没有更新博客了,总感觉要是再不抽空总结总结点东西,分分钟就会被懒惰的状态给打到了.同时也希望同学们谨记,如果你已经决定要坚持某些正确的东西,比如背完某章单词,看一完本书抑或是 ...

  9. Android动画 interpolator的用法

    1. <?xml version="1.0" encoding="utf-8"?> 2. <set 3. xmlns:Android=&quo ...

  10. PN-Traniger

        首先先从Bezier说起: 一条直线上有两个端点,P0和P1,那么直线可以写成 y = kx+b ,其实也就是P(t) = (1-t)P0 + P1 (这是个插值函数),(小注,我时常把这两个 ...