背景:
今天同事在讨论薪水和开支的问题,唉!真服了他们......可能有家庭的人就是不一样吧。我来向来不会参与这样的话题,开支大开支小都是自己用了,所以没什么好说的。不过因此话题我到想起了一个程序:生产者和消费者问题
人物:
LG,TT
故事情节:
LG是一个十分不懂得生活的人,但十分懂得疼爱老婆.TT非常的贤惠,家里的一切都由TT按排好。由于现在通货膨胀,很多企业坚持不下去了。TT公司开始裁员。TT不幸失业了。LG安慰TT说:失业了就失业了,我本来就不想你在外面工作,这么辛苦,要不今年你就别工作了,好好待在家养养身子,明年我们要宝宝。呵呵^_^
TT笑笑,同意了。现在家里只是LG一人工作,成了全职的金钱"生产者"。TT则是家里全职的金钱"消费者"。一年下来,一个只生产,一个只消费,能剩于多少存款呢?下来我就用程序描述生产和消费吧:
首先,家里的钱都是存银行的,所以我们写一个Bank类
Code
1 /**//// <summary>
2 /// 银行类
3 /// </summary>
4 public class Bank
5 {
6 //家庭存帐户
7 float familyAccount = 0;
8
9 //存钱取钱状态
10 bool isGet = false;
11
12 /**//// <summary>
13 /// 存钱方法
14 /// </summary>
15 /// <param name="month">月份</param>
16 /// <param name="salary">存入金额</param>
17 /// <returns></returns>
18 public float SaveMoney(int month, float salary)
19 {
20 lock (this)
21 {
22 if (isGet)//如果现在是不能存的状态,我们就等
23 {
24 try
25 {
26 Monitor.Wait(this);
27 }
28 catch (Exception ex)
29 {
30 Console.WriteLine(ex);
31 }
32 }
33
34 isGet = true;
35 //开始存钱
36 familyAccount += salary;
37
38 Console.WriteLine("LG {0}月份存入{1},现在帐号余额为{2}。", month, salary, familyAccount);
39 Thread.Sleep(500);
40 Monitor.Pulse(this);//银行通知TT,LG发薪水了,存钱了。“我存了钱银行就会通知她。”
41 return familyAccount;
42 }
43 }
44
45 /**//// <summary>
46 /// 取钱方法
47 /// </summary>
48 /// <param name="month">月份</param>
49 /// <param name="money">取出金额</param>
50 /// <returns></returns>
51 public float GetMoney(int month, float money)
52 {
53 lock (this)
54 {
55 if (!isGet)//现在如果不可以取钱,我们就等
56 {
57 try
58 {
59 Monitor.Wait(this);
60 }
61 catch (Exception ex)
62 {
63 Console.WriteLine(ex);
64 }
65 }
66 isGet = false;
67
68 //开始取钱了
69 familyAccount -= money;
70 Console.ForegroundColor = ConsoleColor.Red;
71 Console.WriteLine("{0} {1}月份取走{2},现在帐号余额为{3}。", Thread.CurrentThread.Name,month, money, familyAccount);
72 Console.ResetColor();
73 Thread.Sleep(500);
74 Monitor.Pulse(this);//银行通知LG,TT取钱了。
75 return familyAccount;
76 }
77 }
78 }
下一步我们来描述LG
Code
1 public class LG
2 {
3 //存钱银行
4 private Bank bank;
5 //存钱月份
6 private int months;
7 public LG(Bank bank, int months)
8 {
9 this.bank = bank;
10 this.months = months;
11 }
12
13 /**//// <summary>
14 /// 开始生活,家里一切都由TT打理,LG当然只存了,不会有任何消费
15 /// </summary>
16 public void LifeStart()
17 {
18 for (int i = 1; i <= months; i++)
19 {
20 //因为我的薪水比较低,所以一月到手的只有5000
21 float remnantMoney = bank.SaveMoney(i, 5000);
22 }
23 }
24 }
接下来,描述一下TT
Code
1 public class TT
2 {
3 //取钱银行
4 private Bank bank;
5 //取钱月数,一共没工作多久
6 private int months;
7
8 public TT(Bank bank,int months)
9 {
10 this.bank = bank;
11 this.months = months;
12 }
13
14
15 /**//// <summary>
16 /// 开始生活,TT现在没工作,当然是每个月取钱了!^_^
17 /// </summary>
18 public void LifeStart()
19 {
20 for (int i = 1; i <= months; i++)
21 {
22 Random random = new Random();
23 //TT很省,把家里的开支控制在每年1500-2000之间
24 float money = random.Next(1500, 2000);
25 float remnantMoney = bank.GetMoney(i, money);
26 }
27 }
28 }
最后,我们假设TT一年没有工作,全职消费。LG全职生产
Code
1 public class Program
2 {
3 public static void Main()
4 {
5 //存取银行
6 Bank bank = new Bank();
7
8 //一年12个月
9 TT tt = new TT(bank,12);
10 LG lg = new LG(bank,12);
11
12 Thread ttThread = new Thread(new ThreadStart(tt.LifeStart));
13 ttThread.Name = "TT";
14 Thread lgThread = new Thread(new ThreadStart(lg.LifeStart));
15 lgThread.Name = "LG";
16
17
18 try
19 {
20 //生产
21 lgThread.Start();
22 //消费
23 ttThread.Start();
24
25
26 lgThread.Join();
27 ttThread.Join();
28 }
29 catch (Exception ex)
30 {
31 Console.WriteLine(ex);
32 }
33 Console.ReadKey();
34
35 }
36 }
程序的结果:
此讲到此结束。
由此看出,一年下来,家里的实收不到4W,可怜呀!
有表达不清的地方,望大家指出,谢谢!
作者:Louja
出处:http://louja.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此声明,且在文章页面给出原文连接,否则保留追究法律责任的权利。