11_IEC_T1S1_OOPs_Session_16

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Slide1of31Session16Ver.1.0Object-OrientedProgrammingUsingC#Inthissession,youwilllearnto:ImplementmultiplethreadsIdentifythethreadpriorityUsesynchronizationinthreadsIdentifycommunicationbetweenprocessesDeclaredelegatesInstantiatedelegateObjectivesSlide2of31Session16Ver.1.0Object-OrientedProgrammingUsingC#Demo:HangmanGameProblemStatement:ThenextdoorchildrenrequestyoutocreatetheHangmangameforthem.ThegameasksausertoenteracategoryasBookorMovie.Basedonthecategory,abooknameormovienameisextractedandtheuserisaskedtoguessthenamebygivingthecharacteranditspositioninastring.Auserwillget60secondstoplaythegame.DeveloptheHangmangameapplication.Slide3of31Session16Ver.1.0Object-OrientedProgrammingUsingC#Demo:HangmanGame(Contd.)Solution:Tosolvetheprecedingproblem,youneedtoperformthefollowingtasks:1.Identifythedatasourcewherethenameofthebookormovieisstored.2.Createaconsole-basedapplicationfortheHangmangame.3.Buildandexecutetheapplication.Slide4of31Session16Ver.1.0Object-OrientedProgrammingUsingC#IntroducingMultithreadingMultithreadinghelpstoperformvariousoperationssimultaneouslyandsavesusers’time.Multithreadingallowsyoutoachievemultitaskinginaprogram.Multitaskingistheabilitytoexecutemorethanonetaskatthesametime.Multitaskingcanbedividedintothefollowingcategories:Process-basedmultitaskingThread-basedmultitaskingLetusunderstandmultithreadingwiththehelpofanexample.Slide5of31Session16Ver.1.0Object-OrientedProgrammingUsingC#Aladyisworkingonacomputer.Slide6of31Session16Ver.1.0Object-OrientedProgrammingUsingC#Theladyiseatinganapple,readingabook,andworkingonacomputersimultaneously.Slide7of31Session16Ver.1.0Object-OrientedProgrammingUsingC#AdvantagesofMultithreadingTheadvantagesofmultithreadingare:ImprovedperformanceMinimizedsystemresourceusageSimultaneousaccesstomultipleapplicationsProgramstructuresimplificationSlide8of31Session16Ver.1.0Object-OrientedProgrammingUsingC#LimitationsofMultithreadingThelimitationsofmultithreadingare:RaceconditionDeadlockconditionLockstarvationSlide9of31Session16Ver.1.0Object-OrientedProgrammingUsingC#CreatingMultipleThreadsYoucancreatemultiplethreadsinaprogrambyextendingtheThreadclass.Slide10of31Session16Ver.1.0Object-OrientedProgrammingUsingC#Oneoftheattributesthatcontrolsthebehaviorofathreadisitspriority.The.NETRuntimeEnvironmentexecutesthreadsbasedontheirpriority.Thethreadsarefixed-priorityscheduled.Eachthreadwithitspriorityhasitspositioninthethreadqueueoftheprocessor.IdentifyingtheThreadPrioritySlide11of31Session16Ver.1.0Object-OrientedProgrammingUsingC#Threadpriorityisthepropertythatspecifiesthepriorityofonethreadwithrespecttothepriorityofanotherthread.Threadprioritycanbedefinedas:AbovenormalBelownormalHighestLowestNormalAthreadwithhigherpriorityrunsbeforethreads,whichhavelowerpriority.IfC#encountersanotherthreadwithhigherpriority,thecurrentthreadispushedback,andthethreadwiththehigherpriorityisexecuted.DefiningThreadPrioritySlide12of31Session16Ver.1.0Object-OrientedProgrammingUsingC#YoucansetthethreadpriorityafteritiscreatedusingthePrioritypropertyoftheThreadclass.Thefollowingsyntaxshowshowtosetthethreadpriority:NewThread.Priority=ThreadPriority.Highest;SettingtheThreadPrioritySlide13of31Session16Ver.1.0Object-OrientedProgrammingUsingC#SettingtheThreadPriority(Contd.)NewThread.Priority=ThreadPriority.Highest;ThreadPriority.HighestPropertySpecifiesthenewprioritysettingforathreadSlide14of31Session16Ver.1.0Object-OrientedProgrammingUsingC#Ifmultiplethreadswiththesamepriorityareavailable,theschedulercyclesthroughthethreadsinthatpriority,givingeachthreadafixedtimesliceinwhichtoexecute.Aslongasathreadwithahigherpriorityisavailabletorun,lowerprioritythreadsdonotgettoexecute.Whentherearenomorerunnablethreadsatagivenpriority,theschedulermovestothenextlowerpriorityandschedulesthethreadsatthatpriorityforexecution.SettingtheThreadPriority(Contd.)Slide15of31Session16Ver.1.0Object-OrientedProgrammingUsingC#Inamultithreadedapplication,whenthreadsneedtosharedatawitheachother,theapplicationshouldensurethatonethreaddoesnotchangethedatausedbytheotherthread.C#enablesyoutocoordinatetheactionsofmultiplethreadsbyusingsynchronizedmethodsorsynchronizedstatements.UsingSynchronizationinThreadsSlide16of31Session16Ver.1.0Object-OrientedProgrammingUsingC#Synchronizationofthreadsensuresthatiftwoormorethreadsneedtoaccessasharedresourcethenthatresourceisusedbyonlyonethreadatatime.Youcansynchronizeyourcodeusingthesynchronizedkeyword.Synchronizationisbasedontheconceptofmonitoring.Amonitorisanobjectthatisusedasalocktothedatamembersandmethodsofaclass.SynchronizingThreadsSlide17of31Session16Ver.1.0Object-OrientedProgrammingUsingC#Thefollowingfigureshowshowsynchronizationismaintainedamongthreads.SynchronizingThreads(Contd.)Slide18of31Session16Ver.1.0Object-OrientedProgrammingUsingC#LockingCodeUsingtheMonitorLocksTheSystem.Monitorclassenablesyoutoserializetheaccesstoblocksofcodebymeansoflocksandsignals.Slide19of31Session16Ver.1.0Object-OrientedProgrammingUsingC#UsingMonitorLockswiththeC#LockStatementTheotherwaytolockcodeisbyusingtheC#lockst

1 / 31
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功