Create a gist now

Instantly share code, notes, and snippets.

I/O-bound tasks for experimenting in the Scala REPL
def p(s: String) {
for (i <- 1 to 10) {
Thread.sleep(500)
println(i + ": " + s)
}
}
// running two tasks sequentially
// about 20 seconds by design
val t0 = System.currentTimeMillis
p("a")
p("b")
p("c")
p("d")
System.currentTimeMillis - t0
// running two tasks concurrently
// down to about about 5 seconds
val t0 = System.currentTimeMillis
val p1 = new java.lang.Thread { override def run() { p("a") } }
val p2 = new java.lang.Thread { override def run() { p("b") } }
val p3 = new java.lang.Thread { override def run() { p("c") } }
val p4 = new java.lang.Thread { override def run() { p("d") } }
p1.start()
p2.start()
p3.start()
p4.start()
p1.join()
p2.join()
p3.join()
p4.join()
System.currentTimeMillis - t0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment