- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Fwd: Questions about Actor pattern (wrapping public methods into actor messages for single threaded access)
I'm pondering moving my lock based code in scala to an actor oriented approach. The goal would be to make sure anything modifying an object's internal state would be run single threaded. Below is one way I could reduce locking while still having a public "API" opposed to simply allowing messages to be sent. Does anyone have any ideas on other ways to accomplish this, and the pros and cons of using this method? Thanks in advance!
---------------------------------------------------------------------------------------------
import scala.actors.Actorimport scala.actors.Actor._
class User { private var _state = 0 def addState() = sendInternalMessage { _state = _state + 1 }
def subtractState() = sendInternalMessage { var tempState = _state Thread.sleep(500) //still shouldn't be a race condition... _state = tempState -1 }
def state = _state
private def sendInternalMessage(f: =>Unit) = myActor ! f private val myActor:Actor = actor { loop { react { case f:( ()=>Unit ) => f } } }}---------------------------------------------------------------------------------------------
--Vincent










Re: Fwd: Questions about Actor pattern (wrapping public methods
This is how we worked in many cases (we even had an EvalActor class with 'eval' method). But note that you have a bug: '!' is strict so 'f' is evaluated before the message is sent.
Vincent Marquez wrote:
Re: Fwd: Questions about Actor pattern (wrapping public methods
--Vincent
On Tue, Dec 27, 2011 at 8:21 PM, Ittay Dror <ittay [dot] dror [at] gmail [dot] com> wrote:
Re: Fwd: Questions about Actor pattern (wrapping public methods
Have you looked at Akka's typed actors? They should do what you want.
The way I do this kind of thing is put the state in the actor itself, and send messages that trigger actions on that state, or contain functions that modify the state.
On Dec 27, 2011 4:20 PM, "Vincent Marquez" <vincent [dot] marquez [at] gmail [dot] com> wrote:Questions about Actor pattern (wrapping public methods into acto
---------------------------------------------------------------------------------------------
import scala.actors.Actorimport scala.actors.Actor._
class User { private var _state = 0 def addState() = sendInternalMessage { _state = _state + 1 }
def subtractState() = sendInternalMessage { var tempState = _state Thread.sleep(500) //still shouldn't be a race condition... _state = tempState -1 }
def state = _state
private def sendInternalMessage(f: =>Unit) = myActor ! f private val myActor:Actor = actor { loop { react { case f:( ()=>Unit ) => f } } }}---------------------------------------------------------------------------------------------
--Vincent
Re: Questions about Actor pattern (wrapping public methods into
Am Mittwoch, 28. Dezember 2011 03:38:24 UTC+1 schrieb Vincent Marquez:
sending code to the actor always feels a bit backwards to me: an actor is state+behavior, i.e. it contains the behavior. Countering with Akka syntax:
class User extends Actor {
var state = 0
def receive = {
case Add(x) => state += x
case Sub(x) => state -= x
case Get => sender ! state
}
}
Please note that Akka actors are encapsulated behind ActorRef (i.e. nobody can access the actual object and state does not need to be private). I left out the Thread.sleep() because it does not change anything (besides throughput). Of course this works best if you have more than only this part converted so that the whole processing can use message sends.
Regards,
Roland
Re: Questions about Actor pattern (wrapping public methods into
Am Donnerstag, 29. Dezember 2011 19:15:31 UTC+1 schrieb rkuhn:
Re: Questions about Actor pattern (wrapping public methods into
unless you need massive parallel execution, i would think that using Scala-STM ( http://nbronson.github.com/scala-stm/ ) is much simpler for achieving thread-safety with your state
import concurrent.stm._
class User {
private val _state = Ref( 0 )
def addState()( implicit tx: InTxn ) { _state += 1 }
def subtractState()( implicit tx: InTxn ) { _state -= 1 }
def state( implicit tx: InTxn ) = _state()
}
best, -sciss-
On 28 Dec 2011, at 03:38, Vincent Marquez wrote:
> I'm pondering moving my lock based code in scala to an actor oriented approach. The goal would be to make sure anything modifying an object's internal state would be run single threaded. Below is one way I could reduce locking while still having a public "API" opposed to simply allowing messages to be sent. Does anyone have any ideas on other ways to accomplish this, and the pros and cons of using this method? Thanks in advance!
>
>
> ---------------------------------------------------------------------------------------------
> import scala.actors.Actor
> import scala.actors.Actor._
>
> class User {
> private var _state = 0
>
> def addState() = sendInternalMessage {
> _state = _state + 1
> }
>
> def subtractState() = sendInternalMessage {
> var tempState = _state
> Thread.sleep(500) //still shouldn't be a race condition...
> _state = tempState -1
> }
>
> def state = _state
>
> private def sendInternalMessage(f: =>Unit) =
> myActor ! f
>
> private val myActor:Actor = actor {
> loop {
> react {
> case f:( ()=>Unit )
> => f
> }
> }
> }
> }
> ---------------------------------------------------------------------------------------------
>
> --Vincent
>
Re: Questions about Actor pattern (wrapping public methods into
--Vincent
On Wed, Dec 28, 2011 at 12:05 AM, Sciss <contact [at] sciss [dot] de> wrote: