Created
January 13, 2018 00:08
-
-
Save lucasrpb/d45a07a2cdbb7640d0c066e497ef9f26 to your computer and use it in GitHub Desktop.
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 characters
| import org.scalatest.FlatSpec | |
| import scala.concurrent.{Await, Future, Promise} | |
| import scala.concurrent.duration._ | |
| import scala.concurrent.ExecutionContext.Implicits.global | |
| class MainSpec extends FlatSpec { | |
| "accounts balances" should "be equal before and after transfers" in { | |
| val m = 3 | |
| var tasks = scala.collection.mutable.Buffer[Future[Boolean]]() | |
| var threads = scala.collection.mutable.Buffer[Long]() | |
| def job(i: Int): Future[Unit] = { | |
| Future { | |
| val tid = Thread.currentThread().getId | |
| threads += tid | |
| Thread.sleep(2000) | |
| println(s"\nTID: ${tid}, time: ${System.currentTimeMillis()}, time: ${System.currentTimeMillis()}\n") | |
| } | |
| } | |
| /*// Parallel workload. Call and chain after... | |
| val f1 = job(0) | |
| val f2 = job(1) | |
| val f3 = job(2) | |
| val f = f1.flatMap(_ => f2).flatMap(_ => f3) //Future.sequence(Seq(f1, f2, f3)) | |
| */ | |
| // Serial execution (chaining first) or: job(0).flatMap(_ => f2) | |
| val f = for { | |
| f1 <- job(0) | |
| f2 <- job(1) | |
| f3 <- job(2) | |
| } yield { | |
| println("the end!") | |
| } | |
| // or val f = job(0).flatMap(_ => job(1)).flatMap(_ => job(2)) | |
| println("passou!") | |
| Await.ready(f, 10 seconds) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment