Skip to content

Instantly share code, notes, and snippets.

@pei0804
Created December 30, 2018 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pei0804/7501efe6230bb96b8628e7d89343b390 to your computer and use it in GitHub Desktop.
Save pei0804/7501efe6230bb96b8628e7d89343b390 to your computer and use it in GitHub Desktop.
ScalaFuture
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Failure, Random, Success}
object Example1 extends App {
def sleep(time: Long) { Thread.sleep(time) }
implicit val baseTime = System.currentTimeMillis
// 別スレッドで計算開始
val f = Future {
sleep(500)
1 + 1
}
// 上の計算が終了するまで待つ
val result = Await.result(f, 1 second)
println(result)
}
object Example2 extends App {
def sleep(time: Long) { Thread.sleep(time) }
println("starting calculation ...")
val f = Future {
sleep(Random.nextInt(500))
42
}
println("before onComplete")
f.onComplete {
case Success(value) => println(s"Got the callback, meaning = $value")
case Failure(e) => e.printStackTrace
}
// do the rest of your work
println("A ..."); sleep(100)
println("B ..."); sleep(100)
println("C ..."); sleep(100)
println("D ..."); sleep(100)
println("E ..."); sleep(100)
println("F ..."); sleep(100)
sleep(2000)
}
object OnSuccessAndFailure extends App {
def sleep(time: Long) { Thread.sleep(time) }
val f = Future {
sleep(Random.nextInt(500))
if (Random.nextInt(500) > 250) throw new Exception("Yikes!") else 42
}
f onSuccess {
case result => println(s"Success: $result")
}
f onFailure {
case t => println(s"Exception: ${t.getMessage}")
}
// do the rest of your work
println("A ..."); sleep(100)
println("B ..."); sleep(100)
println("C ..."); sleep(100)
println("D ..."); sleep(100)
println("E ..."); sleep(100)
println("F ..."); sleep(100)
sleep(2000)
}
object Futures2 extends App {
def sleep(time: Long) { Thread.sleep(time) }
implicit val baseTime = System.currentTimeMillis
def longRunningComputation(i: Int): Future[Int] = Future.apply {
sleep(100)
i + 1
}
// this does not block
longRunningComputation(11).onComplete {
case Success(result) => println(s"result = $result")
case Failure(e) => e.printStackTrace
}
// important: keep the jvm from shutting down
sleep(1000)
}
object RunningMultipleCalcs extends App {
def sleep(time: Long) { Thread.sleep(time) }
object Cloud {
def runAlgorithm(i: Int): Future[Int] = Future.apply {
sleep(Random.nextInt(500))
val result = i + 10
println(s"returning result from cloud: $result")
result
}
}
println("starting futures")
val result1 = Cloud.runAlgorithm(10)
val result2 = Cloud.runAlgorithm(20)
val result3 = Cloud.runAlgorithm(30)
println("before for-comprehension")
val result = for {
r1 <- result1
r2 <- result2
r3 <- result3
} yield (r1 + r2 + r3)
println("before onSuccess")
result onSuccess {
case result => println(s"total = $result")
}
println("before sleep at the end")
sleep(2000) // important: keep the jvm alive
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment