[C#]模拟多人不同面值购票找零的多线程代码
2008-12-08 17:23
169 查看
这篇文章,纯属语言转换文章,引用了他人的思路,修改成C#的线程思想,还有许多不完善的地方,希望在讨论中不断完善.
原来的文章是一个Java写的,作者是java2000_net(老紫竹),一个偶然的机会,我看到了那篇文章,感觉思路挺有趣,就想把他转换成C#的来学习.时间紧凑,到12月初才有空来做.
原文地址:http://blog.csdn.net/java2000_net/archive/2008/10/19/3083836.aspx
public class Test
{
static SalesLady saleslady = new SalesLady(14, 0, 0);
static EventWaitHandle wh = new AutoResetEvent(false);
static void Main()
{
int[] moneies = { 10, 10, 5, 10, 5, 10, 5, 5, 10, 5, 10, 5, 5, 10, 5 };
Thread[] aThreadArray = new Thread[20];
Console.WriteLine("现在开始售票:");
for (int i = 0; i < moneies.Length; i++)
{
CustomerClass cc = new CustomerClass(i + 1, moneies[i]);
aThreadArray[i] = new Thread(cc.run);
aThreadArray[i].Start();
Thread.Sleep(0);
}
for (int i = 0; i < moneies.Length; i++)
{
try
{
aThreadArray[i].Join();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
Console.WriteLine("票已售完" + saleslady.memontoes);
Console.Read();
}
class SalesLady
{
public int memontoes, five, ten;
public SalesLady(int m, int f, int t)
{
memontoes = m;
five = f;
ten = t;
}
public String ruleForSale(int num, int money)
{
String s = null;
if (memontoes <= 0)
return "对不起,已经售完";
if (money == 5)
{
memontoes--;
five++;
s = "给你票,你的钱正好。";
//notifyAll();
wh.Set(); //唤醒
}
else if (money == 10)
{
while (five < 1)
{
try
{
Console.WriteLine("" + num + "号顾客用10元购票,请等待");
//wait();
wh.WaitOne(); //
Thread.Sleep(1);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
// 如果你的线程能够运行到这里,那么一定有一个five,此时就看哪个线程先被执行了
// 因为是同步方法,其中获得运行权利的线程,必须运行结束才会让其它的线程运行
// 所以当再次判断时,那个finve又没有了。
// 不会出现没有five却找零的问题。
}
// 如果你的线程能够运行到这里,那么其一定有一个five,
// 所以这个线程不会出现没有five而找零的问题
if (memontoes <= 0)
{
return "对不起,已经售完";
}
memontoes--;
five -= 1;
ten++;
s = "给你票,找你5元。";
}
return s;
}
}
// 顾客
class CustomerClass
{
int num, money;
public void run()
{
Console.WriteLine("我是" + num + "号顾客,用" + money + "元购票,售票员说:"
+ saleslady.ruleForSale(num, money));
}
public CustomerClass(int n, int m)
{
num = n;
money = m;
}
}
}
小结
C#的Console.WriteLine的输出也是有先后顺序的,按Java的那种写法,如果不在WaitOne()后面加一个Sleep(1),会让后面的找零先输出的,虽然逻辑上运行没有问题.在这里修改Sleep里的时间也会影响到后面的卖票顺序的.(谁叫你发呆!^-^)
原来的文章是一个Java写的,作者是java2000_net(老紫竹),一个偶然的机会,我看到了那篇文章,感觉思路挺有趣,就想把他转换成C#的来学习.时间紧凑,到12月初才有空来做.
原文地址:http://blog.csdn.net/java2000_net/archive/2008/10/19/3083836.aspx
public class Test
{
static SalesLady saleslady = new SalesLady(14, 0, 0);
static EventWaitHandle wh = new AutoResetEvent(false);
static void Main()
{
int[] moneies = { 10, 10, 5, 10, 5, 10, 5, 5, 10, 5, 10, 5, 5, 10, 5 };
Thread[] aThreadArray = new Thread[20];
Console.WriteLine("现在开始售票:");
for (int i = 0; i < moneies.Length; i++)
{
CustomerClass cc = new CustomerClass(i + 1, moneies[i]);
aThreadArray[i] = new Thread(cc.run);
aThreadArray[i].Start();
Thread.Sleep(0);
}
for (int i = 0; i < moneies.Length; i++)
{
try
{
aThreadArray[i].Join();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
Console.WriteLine("票已售完" + saleslady.memontoes);
Console.Read();
}
class SalesLady
{
public int memontoes, five, ten;
public SalesLady(int m, int f, int t)
{
memontoes = m;
five = f;
ten = t;
}
public String ruleForSale(int num, int money)
{
String s = null;
if (memontoes <= 0)
return "对不起,已经售完";
if (money == 5)
{
memontoes--;
five++;
s = "给你票,你的钱正好。";
//notifyAll();
wh.Set(); //唤醒
}
else if (money == 10)
{
while (five < 1)
{
try
{
Console.WriteLine("" + num + "号顾客用10元购票,请等待");
//wait();
wh.WaitOne(); //
Thread.Sleep(1);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
// 如果你的线程能够运行到这里,那么一定有一个five,此时就看哪个线程先被执行了
// 因为是同步方法,其中获得运行权利的线程,必须运行结束才会让其它的线程运行
// 所以当再次判断时,那个finve又没有了。
// 不会出现没有five却找零的问题。
}
// 如果你的线程能够运行到这里,那么其一定有一个five,
// 所以这个线程不会出现没有five而找零的问题
if (memontoes <= 0)
{
return "对不起,已经售完";
}
memontoes--;
five -= 1;
ten++;
s = "给你票,找你5元。";
}
return s;
}
}
// 顾客
class CustomerClass
{
int num, money;
public void run()
{
Console.WriteLine("我是" + num + "号顾客,用" + money + "元购票,售票员说:"
+ saleslady.ruleForSale(num, money));
}
public CustomerClass(int n, int m)
{
num = n;
money = m;
}
}
}
小结
C#的Console.WriteLine的输出也是有先后顺序的,按Java的那种写法,如果不在WaitOne()后面加一个Sleep(1),会让后面的找零先输出的,虽然逻辑上运行没有问题.在这里修改Sleep里的时间也会影响到后面的卖票顺序的.(谁叫你发呆!^-^)
相关文章推荐
- 模拟多人不同面值购票找零的多线程代码
- UVa-674 - Coin Change 不同面值找零的方案数
- 利用C# + GDI plus模拟杂乱无章的现实场景
- C#在RichTextBox中显示不同颜色文字的方法
- 【心得体会】c#与java继承的时候,对方法覆盖的不同处理
- Head First C# (蜂巢模拟系统)
- 让C# Excel导入导出 支持不同版本Office
- Java 和C# 不同的地方
- C#中不同窗体之间的数据传递
- C# : WebRequest发起Http Post请求模拟登陆并cookie处理示例
- 用C#模拟 Java Bean (六)—— Demo.zip
- C# 中的身份模拟/帐户模拟(编程方式实现)
- C# 中使用System.Net.Http.HttpClient 模拟登录博客园 (GET/POST)
- C# WinForm (笨方法)根据不同的样式配置 设置窗体相关控件的背景 以改变窗体风格
- c# 模拟连接池
- 利用C#进行socket编程(模拟服务器和客户端通信)
- c# 实现网页上用户自动登陆|asp.net 模拟网站登录
- C、C++、C#简单代码编译结果的不同!
- javascript模拟C#继承练习
- C#模拟Post和Get方式发送数据