19. "Future" must {
import scala.concurrent.ExecutionContext.Implicits.global
"map and flatMap" in {
val futureMessage = Future {
Thread.sleep(1000); 1
}.flatMap(value => Future {
Thread.sleep(1000); value + 1
}).map(s => s"This is a value of future after $s seconds")
Await.result(futureMessage, 5 seconds) must be("This is a value of future after 2 seconds")
}
"for comprehension" in {
val futureMessage = for {
s1 <- Future {
Thread.sleep(1000); 1
}
s2 <- Future {
Thread.sleep(1000); s1 + 1
}
} yield s"This is a value of future after $s2 seconds"
Await.result(futureMessage, 5 seconds) must be("This is a value of future after 2 seconds")
}
}
Future
並列に実行
並列に実行
並列に実行
forコンプリヘンション
並列に実行
並列に実行
22. ParallelCollection
"parallel collection" must {
"behave same as standard one" in {
val list = (0 to 9999).toList
list.map(_ + 1).filter(_ % 2 == 0).fold(0)(_ + _) must be
list.par.map(_ + 1).filter(_ % 2 == 0).fold(0)(_ + _)
}
}
通常のコレクション
並列コレクション
使い方まったく同じ