How do you get an object associated with a Future Actor?

Posted by Bruce Ferguson on Stack Overflow See other posts from Stack Overflow or by Bruce Ferguson
Published on 2011-01-10T19:42:35Z Indexed on 2011/01/10 19:53 UTC
Read the original article Hit count: 207

Filed under:
|
|

I would like to be able to get access to the object that is being returned from spawning a future

import scala.actors.Future
import scala.actors.Futures._

class Object1(i:Int) {
    def getAValue(): Int = {i}
}

object Test {
    def main( args: Array[String] ) = {
        var tests = List[Future[Object1]]()
        for(i <- 0 until 10) {
            val test = future {
                val obj1 = new Object1(i)
                println("Processing " + i + "...")
                Thread.sleep(1000)
                println("Processed " + i)
                obj1
            }
            tests = tests ::: List(test)
        }
        val timeout = 1000 * 60 * 5  // wait up to 5 minutes
        val futureTests = awaitAll(timeout,tests: _*)

        futureTests.foreach(test => println("result: " + future()))
    }
}

The output from one run of this code is:

Processing 0...
Processing 1...
Processing 2...
Processing 3...
Processed 0
Processing 4...
Processed 1
Processing 5...
Processed 2
Processing 6...
Processed 3
Processing 7...
Processed 4
Processing 8...
Processed 6
Processing 9...
Processed 5
Processed 7
Processed 8
Processed 9
result: <function0>
result: <function0>
result: <function0>
result: <function0>
result: <function0>
result: <function0>
result: <function0>
result: <function0>
result: <function0>
result: <function0>

I've tried future().getClass(), and the output is

result: class scala.actors.FutureActor

What I'm looking to be able to access is the obj1 objects.

Thanks

Bruce

© Stack Overflow or respective owner

Related posts about scala

Related posts about future