C#模拟死锁问题
2007-03-28 12:26
477 查看
/// <summary>
/// 模拟学生作笔记,假设一个学生只有同时拥有笔记本和笔才能学习,
/// 这里模拟2个学生,但只有1个笔记本和一只笔
/// 如果想进行学习,需同时获得笔记本和笔,当A同学获得笔记本时,
/// B也已获得笔,此时,A试图获得B的笔,而B试图获得A的笔记本,
/// 此时便发生死锁
/// </summary>
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ThreadTest
{
/// <summary>
/// 模拟学生作笔记,假设一个学生只有同时拥有笔记本和笔才能学习,
/// 这里模拟2个学生,但只有1个笔记本和一只笔
/// 如果想进行学习,需同时获得笔记本和笔,当A同学获得笔记本时,
/// B也已获得笔,此时,A试图获得B的笔,而B试图获得A的笔记本,
/// 此时便发生死锁
/// </summary>
class Program
{
static void Main(string[] args)
{
Tool t1 = new Tool("Book");//笔记本
Tool t2 = new Tool("Pen");//笔
Student a = new Student("A",t1,t2);//A学生
Student b = new Student("B",t2,t1);//B学生
Thread ta = new Thread(new ThreadStart(a.Run));
Thread tb = new Thread(new ThreadStart(b.Run));
ta.Start();
tb.Start();
}
}
public class Tool
{
public string Name;
public Tool(string Name)
{
this.Name = Name;
}
}
class Student
{
private string Name;
private Tool Tool1;
private Tool Tool2;
public Student(string Name,Tool tool1,Tool tool2)
{
this.Name = Name;
this.Tool1 = tool1;
this.Tool2 = tool2;
}
public void Run()
{
while(true)
Study();
}
private void Study()
{
lock (Tool1)//得到并锁住工具1
{
Console.WriteLine("{0} get {1}", Name,Tool1.Name);
lock (Tool2)//得到并锁住工具2
{
Console.WriteLine("{0} get {1}", Name, Tool2.Name);
Console.WriteLine("{0} Will Study", Name);//得到工具并学习
Console.WriteLine("{0} Dorp {1}", Name, Tool1.Name); //释放工具1
}
Console.WriteLine("{0} Dorp {1}", Name, Tool2.Name); //释放工具2
}
}
}
}
运行结果:
--------------------------------
A get Book
B get Pen
--------------------------------
/// 模拟学生作笔记,假设一个学生只有同时拥有笔记本和笔才能学习,
/// 这里模拟2个学生,但只有1个笔记本和一只笔
/// 如果想进行学习,需同时获得笔记本和笔,当A同学获得笔记本时,
/// B也已获得笔,此时,A试图获得B的笔,而B试图获得A的笔记本,
/// 此时便发生死锁
/// </summary>
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ThreadTest
{
/// <summary>
/// 模拟学生作笔记,假设一个学生只有同时拥有笔记本和笔才能学习,
/// 这里模拟2个学生,但只有1个笔记本和一只笔
/// 如果想进行学习,需同时获得笔记本和笔,当A同学获得笔记本时,
/// B也已获得笔,此时,A试图获得B的笔,而B试图获得A的笔记本,
/// 此时便发生死锁
/// </summary>
class Program
{
static void Main(string[] args)
{
Tool t1 = new Tool("Book");//笔记本
Tool t2 = new Tool("Pen");//笔
Student a = new Student("A",t1,t2);//A学生
Student b = new Student("B",t2,t1);//B学生
Thread ta = new Thread(new ThreadStart(a.Run));
Thread tb = new Thread(new ThreadStart(b.Run));
ta.Start();
tb.Start();
}
}
public class Tool
{
public string Name;
public Tool(string Name)
{
this.Name = Name;
}
}
class Student
{
private string Name;
private Tool Tool1;
private Tool Tool2;
public Student(string Name,Tool tool1,Tool tool2)
{
this.Name = Name;
this.Tool1 = tool1;
this.Tool2 = tool2;
}
public void Run()
{
while(true)
Study();
}
private void Study()
{
lock (Tool1)//得到并锁住工具1
{
Console.WriteLine("{0} get {1}", Name,Tool1.Name);
lock (Tool2)//得到并锁住工具2
{
Console.WriteLine("{0} get {1}", Name, Tool2.Name);
Console.WriteLine("{0} Will Study", Name);//得到工具并学习
Console.WriteLine("{0} Dorp {1}", Name, Tool1.Name); //释放工具1
}
Console.WriteLine("{0} Dorp {1}", Name, Tool2.Name); //释放工具2
}
}
}
}
运行结果:
--------------------------------
A get Book
B get Pen
--------------------------------
相关文章推荐
- 一个多线程死锁问题的模拟
- c#模拟登录,cookie问题
- c# 通讯死锁问题 线程同步问题
- 用C#模拟“嗜睡的理发师”问题
- C#模拟点击网页按钮,提交数据有关问题
- 未加索引的外键导致的死锁模拟实验以及问题
- C#模拟读者写者问题
- C# Winform 界面线程的Invoke死锁,以及Application.DoEvent的问题
- C# 模拟emwin编译出问题时的解决办法
- Java语言模拟死锁问题
- 线程-模拟死锁问题
- 用C#线程技术模拟“生产者-消费者”经典进程同步问题
- Java模拟排查线程死锁问题
- C# 线程的问题之:死锁
- 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq
- 马士兵视频笔试题:用一个程序模拟死锁的问题
- 多线程死锁问题的模拟及分析
- 线程中 模拟死锁问题
- 马士兵视频笔试题:用一个程序模拟死锁的问题
- C# 4.0 “修复了”死锁问题