已阅读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安徽国控资本有限公司所属企业四季度社会招聘4人笔试历年常考点试题专练附带答案详解试卷3套
- 2025四川广安市武胜县文创旅游开发有限公司招聘3人(第三次)笔试历年典型考点题库附带答案详解试卷3套
- 2025中国人民保险集团股份有限公司招聘笔试历年常考点试题专练附带答案详解试卷3套
- 恩施 公务员考试试题及答案
- 石灰岩矿开采及综合利用项目风险评估报告
- 老旧小区改造建设工程施工方案
- 大数据处理与分析平台搭建方案
- 北京故宫公务员考试面试试题及答案
- 铜矿资源勘探与评估方案
- 澳门大陆公务员考试试题及答案
- 2025铝合金门窗的合同书范本
- 施工现场各工种安全技术操作规程
- 食品冷冻-课件
- 合成氨企业安全生产安全生产双控方案全套资料方案风险分级管控和隐患排查治理全套资料汇编完整版
- 固相膜免疫分析技术技术课件
- 《装配钳工国家职业技能鉴定指南(中级)》理论考试题库(含答案)
- 《黄瓜栽培》课件
- 生活水泵房重点标准化
- 工程伦理学教学课件
- 惠州PX芳烃抽提介绍
- 煤气水封知识课件
评论
0/150
提交评论