




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
.NET程序实验报告实验名称 :上机实验9班 级 : 学 号 : 学生姓名 : 指导老师 : 数学与计算机学院 2015年 6月2号一、实验目的1.熟悉visual 2010的基本操作方法.1. 认真阅读本章相关内容,尤其是案例.2. 实验前进行程序设计,完成源程序的编写任务.3. 反复操作,直到不需要参考教材,能熟练操作为止.二、实验内容1.要求创建一个银行账户类,一个银行客户类,客户类有账户,从银行客户类派生出儿子和父亲,分别对应的操作为存款和取款 。在主程序中创建两个辅助线程,将存款取款操作分给辅助线程,观察两个线程并发操作的结果并分析。2.在实验1的基础上,采用lock对存款取款进行操作,分析多个线程运行的结果,并分析存在的问题并说明原因。3.在实验2的基础上,对两个主线程同步,实现父亲存款后儿子取款,父亲再存款儿子再取款的操作,观察实验结果并分析原理。using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace Test11 public class Account private int banlance; private object obj = new object(); private bool flag = false; public int Banlance get return banlance; public Account(int banlance) this.banlance = banlance; public void Withdraw(int money) if (flag = false) Monitor.Wait(obj); if (money = banlance) banlance -= money; Console.WriteLine(string.Format(取钱成功: 0 , 余额为: 1, money, banlance); else Console.WriteLine(string.Format(取钱失败: 余额不足); flag = false; Monitor.Pulse(obj); public void Deposit(int money) if (flag = true) Monitor.Wait(obj); banlance += money; Console.WriteLine(string.Format(存钱成功: 0 is deposited, 余额为:1, money, banlance); flag = true; Monitor.Pulse(obj); public abstract class Customer protected Account acc; public Account Acc get return acc; public Customer(Account acc) this.acc = acc; public abstract void DoTransact(int money); public class Son:Customer public Son(Account acc) : base(acc) public override void DoTransact(int money) acc.Withdraw(money); public class Father : Customer public Father(Account acc):base(acc) public override void DoTransact(int money) acc.Deposit(money); using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace Test11 class Program public static Son son; public static Father father; public static Random r = new Random(); static void Main(string args) Account acc = new Account(0); son = new Son(acc); father = new Father(acc); Thread threadSon = new Thread(new ParameterizedThreadStart(SonProc); threadSon.Start(r); Thread threadFather = new Thread(new ParameterizedThreadStart(FatherProc); threadFather.Start(r); public static void FatherProc(object obj) Random r = (Random)obj; for (int i = 0; i 20; i+) father.DoTransact(r.Next(0, 1000); public static void SonProc(object obj) Random r = (Random)obj; for (int i = 0; i 20; i+) son.DoTransact(r.Next(0, 1000); using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace Test11 public class Account private int banlance; private object obj = new object(); private bool flag = false; public int Banlance get return banlance; public Account(int banlance) this.banlance = banlance; public void Withdraw(int money) lock (obj) if (flag = false) Monitor.Wait(obj); if (money = banlance) banlance -= money; Console.WriteLine(string.Format(取钱成功: 0 , 余额为: 1, money, banlance); else Console.WriteLine(string.Format(取钱失败: 余额不足); flag = false; Monitor.Pulse(obj); public void Deposit(int money) lock (obj) if (flag = true) Monitor.Wait(obj); banlance += money; Console.WriteLine(string.Format(存钱成功: 0 is deposited, 余额为:1, money, banlance); flag = true; Monitor.Pulse(obj); public abstract class Customer protected Account acc; public Account Acc get return acc; public Customer(Account acc) this.acc = acc; public abstract void DoTransact(int money); public class Son:Customer public Son(Account acc) : base(acc) public override void DoTransact(int money) acc.Withdraw(money); public class Father : Customer public Father(Account acc):base(acc) public override void DoTransact(int money) acc.Deposit(money); using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace Test11 class Program public static Son son; public static Father father; public static Random r = new Random(); static void Main(string args) Account acc = new Account(0); son = new Son(acc); father = new Father(acc); Thread threadSon = new Thread(new ParameterizedThreadStart(SonProc); threadSon.Start(r); Thread threadFather = new Thread(new ParameterizedThreadStart(FatherProc); threadFather.Start(r); public static void FatherProc(object obj) Random r = (Random)obj; for (int i = 0; i 20; i+) father.DoTransact(r.Next(0, 1
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025内蒙古鄂尔多斯实验室成果转化部招聘3人模拟试卷及参考答案详解一套
- 2025年上半年四川乐山职业技术学院赴四川大学考核招聘10人考前自测高频考点模拟试题带答案详解
- 2025年蚌埠市东方人力资源招聘30人模拟试卷及答案详解(夺冠)
- 2025内蒙古呼和浩特市托克托县补录参加2024年公益性岗位招聘4人考前自测高频考点模拟试题及一套完整答案详解
- 安全培训教室宣传标语课件
- 2025湖北恩施硒茶集团招聘财务人员拟聘对象考前自测高频考点模拟试题及答案详解参考
- 河北省【中职专业高考】2025年中职高考对口升学(理论考试)真题卷【土木建筑大类】模拟练习
- 连带责任保证担保合同范本5篇
- 2025菏泽曹县教育系统公开招聘初级岗位教师(166人)模拟试卷附答案详解(典型题)
- 2025年阜阳界首市“政录企用”人才引进8人模拟试卷及答案详解(考点梳理)
- 6.2《插秧歌》任务式课件2025-2026学年统编版高中语文必修上册
- 航海船舶因应气象预报方案
- 电气班组安全教育培训课件
- 2025司法局招聘司法所协理员历年考试试题与答案
- 戊戌变法课件+2025-2026学年统编版八年级历史上册
- 公司合规管理与检查表模板
- 质量月安全知识培训课件
- 《2025同上一堂思政课》观后感10篇
- SY4201.2-2019石油天然气建设工程施工质量验收规范设备安装塔类检验批表格
- 人大监督法讲解课件
- 2025至2030年中国酒吧市场发展现状调查及投资趋势前景分析报告
评论
0/150
提交评论