typed actors vs futures - how are they different?

346 Views Asked by At

I am reading about typed actors and the interface a typed actor provides looks like this :

trait Squarer {
  def squareDontCare(i: Int): Unit //fire-forget

  def square(i: Int): Future[Int] //non-blocking send-request-reply

  def squareNowPlease(i: Int): Option[Int] //blocking send-request-reply

  def squareNow(i: Int): Int //blocking send-request-reply

  @throws(classOf[Exception]) //declare it or you will get an UndeclaredThrowableException
  def squareTry(i: Int): Int //blocking send-request-reply with possible exception
}

How is this different from a simple Future ?

They really seem similar, the interface is the same for def square(i: Int): Future[Int].

Is it so that typed actors are location transparent (and can be run on other nodes) but Futures are not ?

So typed actors can be thought of as a sort of more restricted form of Futures? In the sense that the Future construction is restricted such that for future construction nothing can be used that cannot come through a wire (not serializable). For example closures (or any function) cannot be passed to typed actors but can be used for constructing Futures.

1

There are 1 best solutions below

1
On BEST ANSWER

I can see the following usecase: suppose your service behind the actor has a mutable state. Since method call is a message and messages are processed sequentially you get synchronisation of service's internal state.

BTW actors by default don't restrict messages to be serializable.