Last active
June 15, 2016 22:43
-
-
Save starkcoffee/9b7bee938628a2bff783bed1f0634b26 to your computer and use it in GitHub Desktop.
Revisions
-
starkcoffee revised this gist
Jun 15, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -24,7 +24,7 @@ In the end a colleague told me to "You need to build your futures in the context of an ExecutionContext that runs them in parallel. If you replace `Future {` with `FuturePool.unboundedPool {` in your code snippet they should be executed in parallel". Thanks Kristof! And lo and behold, that works! */ -
starkcoffee created this gist
Jun 15, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,47 @@ /* I wanted to write a specs2 unit test that proves a function works concurrently. I tried writing a unit test to prove that Futures run concurrently (not always sequentially), just for fun. I ran the test below, expecting count to equal 1, but it always executed the futures in order, even though C1 takes the longest time. I realised, I need to go back to school and learn how futures are executed. */ "prove futures run in parallel" in new Context { var count = 0 Await.result(Future.join(Seq( Future { Thread.sleep(2000); println("c1"); count = 1 }, Future { Thread.sleep(1000); println("c2"); count = 2 }, Future { Thread.sleep(0); println("c3"); count = 3 } ))) count ==== 1 } /* It was hard to know where to start: What is executing the futures? Is it Await.result? Is it the specs2 framework? Some other lauering implicit executor somewhere? What also made it tricky is that I could only find documentation on how Scala Futures are executed, not Twitter Futures :/ In the end a colleague told me to "You need to build your futures in the context of an ExecutionContext that runs them in parallel. If you replace `Future {` with `FuturePool.unboundedPool {` in your code snippet they should be executed in parallel". And lo and behold, that works! */ "prove futures run in parallel for realsies" in new Context { var count = 0 Await.result(Future.join(Seq( FuturePool.unboundedPool { Thread.sleep(2000); println("c1"); count = 1 }, FuturePool.unboundedPool { Thread.sleep(1000); println("c2"); count = 2 }, FuturePool.unboundedPool { Thread.sleep(0); println("c3"); count = 3 } ))) count ==== 1 } /* Remaining mysteries: how many pools am I using now? What is the default execution context? */