Created
August 27, 2019 08:18
-
-
Save JonNorman/9c5db9f4538d51ae4c76ab6ed70e9314 to your computer and use it in GitHub Desktop.
A future exploring the nature of recursive flatMap calls on a Future.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 > 2 start | |
4 > 2 end | |
3 > 10 start | |
3 > 9 start | |
3 > 9 end | |
2 > 10 start | |
2 > 9 start | |
3 > 10 end | |
2 > 10 start | |
2 > 9 start | |
2 > 9 end | |
1 > 10 start | |
1 > 9 start | |
2 > 9 end | |
1 > 10 start | |
1 > 9 start | |
2 > 10 end | |
1 > 10 start | |
1 > 9 start | |
2 > 10 end | |
1 > 10 start | |
1 > 9 start | |
1 > 9 end | |
0 > 10 start | |
1 > 9 end | |
0 > 9 start | |
1 > 10 end | |
0 > 10 start | |
1 > 9 end | |
0 > 9 start | |
1 > 10 end | |
1 > 9 end | |
0 > 10 start | |
0 > 9 start | |
1 > 10 end | |
0 > 10 start | |
1 > 10 end | |
0 > 9 start | |
0 > 9 end | |
0 > 10 end | |
0 > 10 start | |
0 > 9 start | |
0 > 9 end | |
0 > 10 start | |
0 > 9 end | |
0 > 9 start | |
0 > 10 end | |
0 > 10 start | |
0 > 10 end | |
0 > 9 end | |
0 > 9 start | |
0 > 10 start | |
0 > 10 end | |
0 > 9 start | |
0 > 9 end | |
0 > 9 end | |
0 > 10 end | |
0 > 10 end | |
0 > 9 end | |
0 > 10 end | |
0 > 9 end | |
0 > 10 end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object FutureTest extends App { | |
import scala.concurrent.{ExecutionContext, Future} | |
import java.util.concurrent.{ConcurrentLinkedQueue, Executors} | |
implicit val executionContext = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(8)) | |
val fs = List(10, 9) | |
val log = new ConcurrentLinkedQueue[String]() | |
def go(countdown: Int, n: Int): Future[Unit] = | |
Future { | |
log.add(s"$countdown > $n start") | |
Thread.sleep(n * 1000) | |
log.add(s"$countdown > $n end") | |
} flatMap { _ => | |
if (countdown > 0 ) { | |
Future.traverse(fs)(f => go(countdown - 1, f)).map(_ => ()) | |
} else { | |
Future.unit | |
} | |
} | |
go(4, 2).onComplete { | |
case _ => log.forEach(println) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment