您好,欢迎访问三七文档
攀枝花学院实验报告实验课程:VisualC#,NET程序设计实验项目:上机实验9实验日期:2015年6月2日系:数学与计算机学院班级:2013级计算机科学与技术姓名:学号:同组人:指导教师:罗明刚成绩:实验仪器设备,药品,器材:Microsoftvisualstudio2010实验原理:1.熟悉visualstudio.net2010的基本操作方法.1.认真阅读本章相关内容,尤其是案例.2.实验前进行程序设计,完成源程序的编写任务.3.反复操作,直到不需要参考教材,能熟练操作为止.实验内容:1.要求创建一个银行账户类,一个银行客户类,客户类有账户,从银行客户类派生出儿子和父亲,分别对应的操作为存款和取款。在主程序中创建两个辅助线程,将存款取款操作分给辅助线程,观察两个线程并发操作的结果并分析。2.在实验1的基础上,采用lock对存款取款进行操作,分析多个线程运行的结果,并分析存在的问题并说明原因。3.在实验2的基础上,对两个主线程同步,实现父亲存款后儿子取款,父亲再存款儿子再取款的操作,观察实验结果并分析原理。1.usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;namespaceBankAccount{publicclassAccount{privateintbanlance;privateobjectobj=newobject();privateboolflag=false;publicintBanlance{get{returnbanlance;}}publicAccount(intbanlance){this.banlance=banlance;}publicvoidWithdraw(intmoney){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);}publicvoidDeposit(intmoney){if(flag==true)Monitor.Wait(obj);banlance+=money;Console.WriteLine(string.Format(存钱成功:{0}isdeposited,余额为:{1},money,banlance));flag=true;Monitor.Pulse(obj);}}publicabstractclassCustomer{protectedAccountacc;publicAccountAcc{get{returnacc;}}publicCustomer(Accountacc){this.acc=acc;}publicabstractvoidDoTransact(intmoney);}publicclassSon:Customer{publicSon(Accountacc):base(acc){}publicoverridevoidDoTransact(intmoney){acc.Withdraw(money);}}publicclassFather:Customer{publicFather(Accountacc):base(acc){}publicoverridevoidDoTransact(intmoney){acc.Deposit(money);}}}usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;namespaceBankAccount{classProgram{publicstaticSonson;publicstaticFatherfather;publicstaticRandomr=newRandom();staticvoidMain(string[]args){Accountacc=newAccount(0);son=newSon(acc);father=newFather(acc);ThreadthreadSon=newThread(newParameterizedThreadStart(SonProc));threadSon.Start(r);ThreadthreadFather=newThread(newParameterizedThreadStart(FatherProc));threadFather.Start(r);}publicstaticvoidFatherProc(objectobj){Randomr=(Random)obj;for(inti=0;i20;i++){father.DoTransact(r.Next(0,1000));}}publicstaticvoidSonProc(objectobj){Randomr=(Random)obj;for(inti=0;i20;i++){son.DoTransact(r.Next(0,1000));}}}}实验截图:2.usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;namespaceBankAccount{publicclassAccount{privateintbanlance;privateobjectobj=newobject();privateboolflag=false;publicintBanlance{get{returnbanlance;}}publicAccount(intbanlance){this.banlance=banlance;}publicvoidWithdraw(intmoney){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);}}publicvoidDeposit(intmoney){lock(obj){if(flag==true)Monitor.Wait(obj);banlance+=money;Console.WriteLine(string.Format(存钱成功:{0}isdeposited,余额为:{1},money,banlance));flag=true;Monitor.Pulse(obj);}}}publicabstractclassCustomer{protectedAccountacc;publicAccountAcc{get{returnacc;}}publicCustomer(Accountacc){this.acc=acc;}publicabstractvoidDoTransact(intmoney);}publicclassSon:Customer{publicSon(Accountacc):base(acc){}publicoverridevoidDoTransact(intmoney){acc.Withdraw(money);}}publicclassFather:Customer{publicFather(Accountacc):base(acc){}publicoverridevoidDoTransact(intmoney){acc.Deposit(money);}}}usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;namespaceBankAccount{classProgram{publicstaticSonson;publicstaticFatherfather;publicstaticRandomr=newRandom();staticvoidMain(string[]args){Accountacc=newAccount(0);son=newSon(acc);father=newFather(acc);ThreadthreadSon=newThread(newParameterizedThreadStart(SonProc));threadSon.Start(r);ThreadthreadFather=newThread(newParameterizedThreadStart(FatherProc));threadFather.Start(r);}publicstaticvoidFatherProc(objectobj){Randomr=(Random)obj;for(inti=0;i20;i++){father.DoTransact(r.Next(0,1000));}}publicstaticvoidSonProc(objectobj){Randomr=(Random)obj;for(inti=0;i20;i++){son.DoTransact(r.Next(0,1000));}}}3.实验总结此次实验算是C#的最后一次实验了吧,此次实验没有线程难,但是也包括了线程的相关知识,主要还是操作了之前操作的类的相关知识,所以在老师讲的时候大部分也能够听得懂,这学期的C#实验就要告一段落了,自己总结了一下这学期C#的学习,没有人比我更了解自己,就是觉得自己上理论课的时候呢计算老是开小差,但是上实验课的时候都认真的去做,遇到不会的就自己去网上找资料自己解决,除了有些像线程那样的难点,其他的几次实验算是都是自己去完成的.不管怎么样,希望自己不管在学习还是工作中都能以积极认真的态度去对待,这样对谁都好.
本文标题:上机实验7
链接地址:https://www.777doc.com/doc-4694108 .html