Skip to content

Instantly share code, notes, and snippets.

@klaeufer
Last active November 29, 2022 22:57
Show Gist options
  • Save klaeufer/5409800 to your computer and use it in GitHub Desktop.
Save klaeufer/5409800 to your computer and use it in GitHub Desktop.
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 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") } }
val t0 = System.currentTimeMillis
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