简体   繁体   中英

Way to use Futures inside Futures in Scala

I need to perform thousands of tasks simultaneously. So i am thinking of using Futures for this purpose. But each of these thousands tasks are already in Future. Is it good to use future inside future. Or i am doing something wrong here. Each task takes approx 1 ms time to compute. So instead of performing them sequentially i think it is preferable to do in futures. So theoretically it should take approx 1 ms or somewhere around that to complete entire task. But complete execution is taking 150 ms. So does that mean all parallel tasks are taking more than what it is taking individually.

val start = System.nanoTime()

You can use the flatMap() function on futures to compose them, which I guess is what you call "futures inside futures", and it's a great idiomatic way to use futures.

Map express "with the result of this future, trigger this one". For example you could do something like getASpecificEndpointInDb().flatMap(endpointURL => doAGetRequest(endpointURL)) where getASpecificEndpointInDb() returns a Future[String] and doAGetRequest() returns a Future[JsObject]

To answer the second part of your question : even if you have 1000 parallel tasks, your computer can only physically process a small number at any given time. Depending on your machine, if it's, let's say 8, the best it can achieve theorically is then 1000/8 = 125 ms. But as there are some context switching costs, 150ms is totally reasonable. And still a lot better than 1000ms if doing it sequentially :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM