您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > 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
本文标题:11_IEC_T1S1_OOPs_Session_16
链接地址:https://www.777doc.com/doc-1227775 .html