concurrency - Synchronization using AKKA actors in scala -
i trying extend code in way inconsistency can occur , add functionality ensure consistency.
- allow localactor a1 read data remoteactor
- at localactor a1 perform modification
- send modification remoteactor (the update valid if there no change @ remoteactor)
other localactor a2 can modify tree @ remoteactor possible inconsistency how should ensure when localactor have part of resource remoteactor (cxta of tree) should remain unchanged or locked no 1 else can modify it. how should approach problem?
remoteactor code:
class remoteactor extends actor { def receive = { case sendcontexta1() => this.synchronized{ println("from remote actor sender:"+sender+" tree part:"+remoteactor.rtree.cxta) sender ! rcvdcxta1(remoteactor.rtree.cxta) } case rcvdcxta1(cxta) => { println("1. recievecontexta1 before :" + remoteactor.rtree.cxta) remoteactor.rtree.cxta = cxta println("1. recievecontexta1 before :" + remoteactor.rtree.cxta) } case _ => println("unknown msg") } } object remoteactor{ var rtree = rcxt(createrndcxta(1),createrndcxtb(2),1,"") def main(args: array[string]) { val system = actorsystem("remotesystem" , config) val remoteactor = system.actorof(props[remoteactor], name="remote") } }
localactora code
class localactora extends actor{ @throws[exception](classof[exception]) def receive = { case tasksum(t1) => remoteactor ! tasksum(t1) val currentsender = sender() case getcontexta1() => remoteactor ! sendcontexta1() case rcvdcxta1(cxta) => rcvdcxt = cxta println("recieved remote actor:"+ cxta) case taskla1(taska) => rcvdcxt = rcvdcxt ::: localactora.tree.cxta println("1. after rcvd part: "+ rcvdcxt) thread.sleep(1500) rcvdcxt.foreach(ctxa => taska.work(ctxa)) remoteactor ! rcvdcxta1(rcvdcxt) case taskla2(taska) => rcvdcxt = rcvdcxt ::: localactora.tree.cxta println("2. after rcvd part: "+ rcvdcxt) // task of dummy sum rcvdcxt.foreach(ctxa => taska.work(ctxa)) } } object localactora { var tree = rcxt(createrndcxta(2),createrndcxtb(2),1,"") def main(args: array[string]) { val localactora1 = system.actorof(props[localactora], name="locala1") val localactora2 = system.actorof(props[localactora], name="locala2") localactora1 ! start localactora2 ! start localactora1 ! getcontexta1() thread.sleep(500) localactora1 ! taskla1(new dummysum()) localactora2 ! getcontexta1() thread.sleep(500) localactora2 ! taskla2(new dummysum()) } }
Comments
Post a Comment