-2

I got a solution for my problem which is not working. I am using Future and collection but it finish very fast which is wrong because it is not runnung the function

myCollection.foreach(element => Future {
  myFunction()
})

what is wrong with my future implementation ?

Omid
  • 1,959
  • 25
  • 42
  • Can you explain what you're actually trying to do? It's nearly impossible to answer this without some context. What does `myFunction` do? Does it need to return a value? That block of code above **will** execute immediately, as it's dispatching the work within `Future`s. – Michael Zajac Jul 07 '14 at 23:42
  • 2
    `Future`s are executed by other threads, current thread won't block at all. – jilen Jul 07 '14 at 23:54
  • it doesn't return any value, it just parse some sentence using a third part library ... I used .par and it speed up 3 times but I wanted to test Future ... and it is not working @LimbSoup – Omid Jul 08 '14 at 00:33
  • What do you mean by *it's not working*? What is it *supposed* to be doing? – Michael Zajac Jul 08 '14 at 00:50
  • 1
    Use `Future.traverse(myCollection)(e => Future(f(e)))` to get a Future back which you can block on, test completion of, etc. If you don't block, your test program will exit without the futures completing. Search SO for more info. – som-snytt Jul 08 '14 at 01:15
  • @LimbSoup suppose to run the function!! – Omid Jul 08 '14 at 02:33
  • @som-snytt foreach doesn't block in completion of futures ? – Omid Jul 08 '14 at 02:35
  • @som-snytt `traverse` is also not working! – Omid Jul 08 '14 at 03:57
  • More answers http://stackoverflow.com/q/13097754/1296806 which was a long time ago now and http://stackoverflow.com/q/20108523/1296806 – som-snytt Jul 08 '14 at 06:14

1 Answers1

0

Read up on the Future api at the Scala docs site and this will be clear as crystal to you: http://docs.scala-lang.org/overviews/core/futures.html

johanandren
  • 11,249
  • 1
  • 25
  • 30