Program freezes when using multiple futures with multiple remote participants

I run two remote actors on the same host, which simply echo them. Then I create another actor that sends a certain amount of messages (using !!) for both participants and storing List of Future objects containing responses from these participants. Then I iterate over this list, getting the result of each Future. The problem is that most of the time some futures never return, I even thought that the actor claims that he sent the answer. The problem occurs randomly, sometimes it goes through the entire list, but most of the time it gets stuck at some point and hangs endlessly.

Here is the code that creates the problem on my machine:

Sink.scala:

import scala.actors.Actor
import scala.actors.Actor._
import scala.actors.Exit
import scala.actors.remote.RemoteActor
import scala.actors.remote.RemoteActor._

object Sink {
  def main(args: Array[String]): Unit = {
     new RemoteSink("node03-0",43001).start()
     new RemoteSink("node03-1",43001).start()
   }
}
class RemoteSink(name: String, port: Int) extends Actor
{
 def act() {
    println(name+" starts")
    trapExit=true
    alive(port)
    register(Symbol(name),self)

    loop {
        react {
            case Exit(from,reason) =>{
                    exit()
            }
            case msg => reply{
                    println(name+" sending reply to: "+msg)
                    msg+" back at you from "+name
                }
        }
    }
 }
}

Source.scala:

import scala.actors.Actor
import scala.actors.Actor._
import scala.actors.remote.Node;
import scala.actors.remote.RemoteActor
import scala.actors.remote.RemoteActor._

object Source {
    def main(args: Array[String]):Unit = {
        val peer = Node("127.0.0.1", 43001)
        val source = new RemoteSource(peer)
        source.start()
    }
}
class RemoteSource(peer: Node) extends Actor
{
    def act() {
        trapExit=true
        alive(43001)
        register(Symbol("source"),self)

        val sinks = List(select(peer,Symbol("node03-0"))
                                   ,select(peer,Symbol("node03-1"))
                                )
        sinks.foreach(link)

        val futures = for(sink <- sinks; i <- 0 to 20) yield    sink !! "hello "+i
        futures.foreach( f => println(f()))

        exit()
    }
}

?

+5
2

, :

futures.foreach( f => println(f()))

, . , , , . , . :

futures.foreach(f => f.foreach(r => println(r)))

:

for (future <- futures; result <- future) { println(result) }

, .

+2

. , future.get . java.lang.Error vs java.lang.NoSuchMethodError. .

0

All Articles