3

Now I wanna an actor to send messages to other actors and at the same time receive messages from others. It seems I need use multi threads in Akka. Below is my code:

def receive = {
    case Rumor => {     
        count+=1; 
            if ...
            else self ! Sleep(FiniteDuration(20, "millis"))
    }
    case Sleep(duration) => {
        case object WakeUp
        context.system.scheduler.scheduleOnce(duration, self, WakeUp)
        context.become(
        {
            case WakeUp => context.unbecome()
                           others ! Rumor
        }, discardOld = false   
        )
    }       
    case _=> .....
 }

my problem are:

1) I am not sure my code would work as I expect. Reference use Akka scheduler inside an actor

2) I already import

import scala.math._
import akka.actor._
import scala.util.Random
import scala.concurrent.duration._

but the compiler still reports error on:

error: Cannot find an implicit ExecutionContext, either require one yourself or import ExecutionContext.Implicits.global
        context.system.scheduler.scheduleOnce(duration, self, WakeUp)
1
  • 2
    Asking I want actor to send messages to other actors and at the same time receive messages from others is the same as How can I shoot myself in the leg. You're exchanging every point of actor model for a huge pile of problems. Scala isn't built to be actors only (like Erlang was) so if you really need to use plain-old-java-concurrency with bare threads and whistles, use it. Commented Oct 4, 2013 at 8:20

3 Answers 3

20

You're doing the whole thing wrong. Actors are designed specifically to remove most problems caused by manual thread management. One of the features for this is that actors always work sequentially. You cannot force an actor to process more than one message at a time. Akka even provides several guarantees about messages and their order. This allows complete thread safety, if you don't do anything stupid, like using mutable messages or calling other actor objects directly.

Sure, you can run different actors in separate threads (using correct dispatcher), and they really will process their messages in parallel. But you cannot run single actor from multiple threads, whatever you do.

You should really read excellent Akka documentation, especial its section on general concepts. You won't have questions like this if you read and understand it.

Sign up to request clarification or add additional context in comments.

Comments

0

Try importing ExecutionContext.Implicits.global

1 Comment

it does not work. But I add "import context.dispatcher", it works. Now my problem is this method can not work as I expect. Do you have any idea? Thanks
0

Use a Router: e.g.

val router1 = system.actorOf(Props[SomeMultiThreadActorClass].withRouter(RoundRobinRouter(nrOfInstances = 5)))

Consider separate application domains (if your platform allows for it) to avoid one thread adversely affecting another one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.