I'm experimenting with futures. So i created a huge list of random numbers and then separated it in 3 groups to run them parallelly with some code
val itemsInGroup = 500000
val numbers: List[Int] = 1.to(1500000).map(v => Random.nextInt(20)).toList
val groups: List[List[Int]] = numbers.grouped(itemsInGroup).toList.take(3)
val future = Future.sequence(groups.map(gr => Future[Int] {countSum(gr)}))
future andThen {
case Success(threeNumbers) => println(threeNumbers)
}
what makes countSum is not too much important, just to take time i use this code
case class Person(name: String, age: Int) {
def age10: Int = age - age % 10
}
def countSum(lst: List[Int]): Int = lst.map(v => Person("John", v).age10).sum
as a result of a future i print list of 3 numbers. The problem is it doesnt work every time. Sometimes andThen works sometimes not, and if i change itemsInGroup value to small amounts, it works more frequently than big amounts of elements. So i suspect that there's a kind of implicit timeout or something otherwise i can't explain such phenomen.
Please, your tips appreciated
UPD Actually so much code were not needed, even simple example
val ft = Future {
Thread.sleep(10)
10
}
ft andThen {
case Success(value) => println("Here i work")
}
works the same way - sometimes works sometimes not, the more delay the less possibility that it will complete