Last active
August 29, 2015 14:07
-
-
Save moccos/61ded2749d296cd72d23 to your computer and use it in GitHub Desktop.
Check Scala's default execution context.
This file contains 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._ | |
import ExecutionContext.Implicits.global | |
import scala.concurrent.duration._ | |
object FutureChecker { | |
val default_n_future = 10 | |
def createFuture(n: Int, base_time: Long, use_block: Boolean): Future[Int] = { | |
var start: Long = 0 | |
val f = Future { | |
start = System.currentTimeMillis() - base_time | |
use_block match { | |
case false => Thread.sleep(200) | |
case true => blocking { Thread.sleep(200) } | |
} | |
n | |
} | |
f onSuccess { | |
case n => | |
val end = System.currentTimeMillis() - base_time | |
val id = Thread.currentThread().getId() | |
println(f"Finished: $n%02d [$start% 5d -> $end% 5d] at $id% 2d") | |
} | |
f | |
} | |
def printSystemInfo() = { | |
println("Processors: " + Runtime.getRuntime().availableProcessors()) | |
println("Thread active: " + Thread.activeCount) | |
println("Current Thread: " + Thread.currentThread().getId()) | |
} | |
def createFutureList(n: Int, use_block: Boolean) = { | |
val base_time = System.currentTimeMillis() | |
Range(0, n).map(nn => { | |
createFuture(nn, base_time, use_block) | |
}) | |
} | |
def main(args: Array[String]): Unit = { | |
// short-handed argument parser | |
val n_future = try { | |
args(0).toInt match { | |
case n if n > 0 => n | |
case _ => default_n_future | |
} | |
} catch { | |
case _: Throwable => default_n_future | |
} | |
val use_blocking = try { | |
args(1).length > 0 | |
} catch { | |
case _: Throwable => false | |
} | |
// start processing | |
val futures = Future.sequence(createFutureList(n_future, use_blocking)) | |
Thread.sleep(20) | |
printSystemInfo() // print after creating futures | |
use_blocking match { | |
case true => println("Mode: sleep with blocking{}.") | |
case false => println("Mode: sleep without blocking{}.") | |
} | |
Await.result(futures, 5.seconds) | |
} | |
} |
This file contains 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
% scala FutureBehaviorChecker.scala 12 | |
Processors: 8 | |
Thread active: 9 | |
Current Thread: 1 | |
Mode: sleep without blocking{}. | |
Finished: 00 [ 32 -> 236] at 10 | |
Finished: 06 [ 34 -> 234] at 16 | |
Finished: 01 [ 34 -> 234] at 11 | |
Finished: 05 [ 34 -> 234] at 15 | |
Finished: 04 [ 34 -> 236] at 14 | |
Finished: 07 [ 35 -> 235] at 17 | |
Finished: 03 [ 34 -> 236] at 13 | |
Finished: 02 [ 34 -> 234] at 12 | |
Finished: 10 [ 243 -> 443] at 11 | |
Finished: 09 [ 243 -> 443] at 13 | |
Finished: 08 [ 243 -> 443] at 12 | |
Finished: 11 [ 244 -> 444] at 12 | |
scala FutureBehaviorChecker.scala 12 0.87s user 2.47s system 58% cpu 5.764 total | |
% scala FutureBehaviorChecker.scala 12 1 | |
Processors: 8 | |
Thread active: 14 | |
Current Thread: 1 | |
Mode: sleep with blocking{}. | |
Finished: 02 [ 34 -> 234] at 12 | |
Finished: 09 [ 34 -> 235] at 19 | |
Finished: 00 [ 29 -> 234] at 10 | |
Finished: 04 [ 34 -> 234] at 22 | |
Finished: 05 [ 34 -> 234] at 14 | |
Finished: 11 [ 39 -> 240] at 21 | |
Finished: 07 [ 34 -> 234] at 16 | |
Finished: 08 [ 34 -> 234] at 15 | |
Finished: 03 [ 33 -> 234] at 13 | |
Finished: 01 [ 33 -> 234] at 11 | |
Finished: 06 [ 34 -> 234] at 18 | |
Finished: 10 [ 35 -> 240] at 20 | |
scala FutureBehaviorChecker.scala 12 1 0.87s user 2.38s system 59% cpu 5.511 total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment