Created
March 26, 2015 03:54
-
-
Save tototoshi/c4b1c73a0d3d498b11d9 to your computer and use it in GitHub Desktop.
Future.successful と Future.apply
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 scala.concurrent.Future | |
// def successful[T](result: T): Future[T] を使った場合 | |
// result を評価する時点でブロックされるので | |
// a b c の順に表示される | |
println("a") | |
val f1 = Future.successful { Thread.sleep(1000); println("b") } | |
println("c") | |
// def apply[T](body: ⇒ T)(implicit execctx: ExecutionContext): Future[T] | |
// body は名前渡しなので評価は遅延され、別スレッドで行われる | |
// 別スレッドを使うのでスレッドプール(ExecutionContext)が必要 | |
// a c の順に表示される | |
// b は(たぶん)表示されない | |
println("a") | |
import scala.concurrent.ExecutionContext.Implicits.global | |
val f2 = Future { | |
Thread.sleep(1000) | |
println("b") | |
} | |
println("c") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment