Skip to content

Instantly share code, notes, and snippets.

@tototoshi
Created March 26, 2015 03:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tototoshi/c4b1c73a0d3d498b11d9 to your computer and use it in GitHub Desktop.
Save tototoshi/c4b1c73a0d3d498b11d9 to your computer and use it in GitHub Desktop.
Future.successful と Future.apply
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