Sometimesitisusefullifwecacapturethestateoftheapplicatio,itsstackoffuctiocalls,whichicludeslocalvariables,theglobalvariablesadtheprogramcouter,adsavethemitoaobject.Ifthisobjectwouldgiveustheabilitytorestarttheprocessigfromthepoitstorediit.
Acotiuatioisexactlythetypeofobjectthatweeed.Thikofacotiuatioasaobjectthat,foragivepoitiyourprogram,cotaisasapshotofthestacktrace,icludigallthelocalvariables,adtheprogramcouter.Youcaotolystorethesethigsithecotiuatioobject,butalsorestoretheexecutiooftheprogramfromacotiuatioobject.Thismeasthatthestacktraceadtheprogramcouteroftheruigprogrambecometheoesstorediacotiuatio.
Cotiuatiosarepowerfulcoceptsfromtheworldoffuctioallaguages,likeScheme,buttheyarebecomigpopulariotherlaguagesaswell.
示例代码:
classMyRuableimplemetsRuable{publicvoidru(){System.out.pritl("started!");for(iti=0;i<10;i++)echo(i);}privatevoidecho(itx){System.out.pritl(x);Cotiuatio.susped();}}Cotiuatioc=Cotiuatio.startWith(ewMyRuable());System.out.pritl("returedacotiuatio");






评论